17 Replies Latest reply on May 23, 2014 11:34 AM by Hari Pala

    REST API in PowerShell

    triple5_555

      I have been able to use the OrionSDK to create PowerShell scripts to help automate some processes that we have. The downside is that each team member will need to install the SDK in order to run the scripts. To try and alleviate this problem, I started looking into the REST/JSON API examples. Is my assumption that using REST will no longer require having the OrionSDK installed?

       

      Has anyone been able to create any valid REST/JSON queries in PowerShell? I am having difficulty getting started. I found the PowerShell command: Invoke-RestMethod -uri https://IPAddress:17778/SolarWinds/InformationService/v3/Json/Query  but have been unable to have this appropriately connect to/authenticate with our Orion Server. Any help getting started will be much appreciated, I think figuring everything else out from there should be fairly simple.

        • Re: REST API in PowerShell
          triple5_555

          I have been able to get a basic request to work with this, using the following commands:

           

          add-type @"

              using System.Net;

              using System.Security.Cryptography.X509Certificates;

              public class TrustAllCertsPolicy : ICertificatePolicy {

                  public bool CheckValidationResult(

                      ServicePoint srvPoint, X509Certificate certificate,

                      WebRequest request, int certificateProblem) {

                      return true;

                  }

              }

          "@

          [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

          $webClient = New-Object System.Net.WebClient

          $realquery = "https://localhost:17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT+NodeID,Caption+FROM+Orion.Nodes+WHERE+Nodes.Vendor='Windows'+AND+Nodes.UnManaged='False'"

          $webClient.Credentials = New-Object System.Net.NetworkCredential("Admin","")

          $results = $webClient.DownloadString($realquery)

          $res = $results | ConvertFrom-Json | select -ExpandProperty results

           

          The problem I am having is with the credentials/authentication. I am only able to connect with a local Orion account, not with an Active Directory account that has the same rights. Is there a setting somewhere that needs to be changed to allow Active Directory accounts to authenticate with the REST API?

            • Re: REST API in PowerShell
              triple5_555

              Way too many complications to get this working in my opinion. The reason AD accounts weren't working was the SolarWinds Information Service V3 was running as local system. Switched that to an account that had rights to AD and everything is pretty smooth sailing. Now that I can query, I'll be moving onto updating entries like I had planned and hopefully not have as many issues. If anyone has examples with powershell and using POST to send parameters, that might save me a lot more time instead of figuring this out on my own piece by piece. Thanks!

              1 of 1 people found this helpful
                • Re: REST API in PowerShell
                  tdanner

                  I can't help but observe that you are making this harder on yourself by not using the powershell snapin. If installing the SDK msi on the machines where you want to run the scripts is too much of a burden, have you considered just passing around the DLLs with the scripts and having them register the snapin if it isn't already registered?

                    • Re: REST API in PowerShell
                      triple5_555

                      Passing DDLs around to 400+ machines to register the snapin is something our team has been trying to avoid. REST API is something we believe to be something big in the future and want to get a head start on figuring it all out. I also can't help but notice that you were probably the one providing the REST API examples in the OrionSDK Documentation, "swis://tdanner-dev.swdev.local". With this, we can also be certain that there aren't different versions of the DLLs out there and don't have to check for that.

                       

                      With a whole lot of poking around and help from a coworker, we have come up with a very portable (no longer requires the SDK) powershell implementation of the Orion REST API. Thought I'd share it below if anyone else was tearing their hair out like we were:

                       

                      $orionCred = Get-Credential

                      add-type @"

                          using System.Net;

                          using System.Security.Cryptography.X509Certificates;

                          public class TrustAllCertsPolicy : ICertificatePolicy {

                              public bool CheckValidationResult(

                                  ServicePoint srvPoint, X509Certificate certificate,

                                  WebRequest request, int certificateProblem) {

                                  return true;

                              }

                          }

                      "@

                      [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

                       

                      ####Query Request####

                      ###Gets the Caption and nodeID from all windows nodes###

                      $verifyquery = "https://"+$orion_server+":17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT Caption,NodeID FROM Orion.Nodes WHERE Nodes.Vendor='Windows'"

                      $results = Invoke-RestMethod -Uri $verifyquery -Credential $orioncred -ContentType "application/json"

                      $res = $results | select -ExpandProperty results

                      #####################

                       

                      ####Update Request####

                      ###Gets the URI of all Windows nodes and does a mass update###

                      $query = "https://"+$orion_server+":17778/SolarWinds/InformationService/v3/Json/Query?query=SELECT URI FROM Orion.Nodes WHERE Nodes.Vendor='Windows'"

                      $results = Invoke-RestMethod -Uri $query -Credential $orioncred -ContentType "application/json"

                      $URIs = ($results | select -ExpandProperty results).URI

                      $body = @{

                        UnmanageFrom=$start

                        UnmanageUntil=$finish

                      }

                      $jsonBody = ConvertTo-Json $body

                      foreach ($uri in $URIs) {

                        $updateQuery = $query = "https://"+$orion_server+":17778/SolarWinds/InformationService/v3/Json/$uri"

                        $results = Invoke-RestMethod -Uri $query -Credential $orioncred -ContentType "application/json" -Body $jsonBody -Method Post

                      }

                      ######################

                        • Re: REST API in PowerShell
                          sw_ross

                          This looks very interesting.  Is there a way to learn more about the REST api for solarwinds?

                           

                          Can it be used to add nodes to solarwinds?

                            • Re: REST API in PowerShell
                              triple5_555

                              There is a little bit of documentation that comes with the OrionSDK. I believe the default path to it is C:\Program Files (x86)\SolarWinds\Orion SDK\Documentation\Orion SDK.pdf. There are a few examples in there that might be enough to get you started.

                               

                              From what I can assume, yes, you can use it to add nodes to solarwinds. We've been able to do it with the powershell commands, it shouldn't be too difficult to convert them to REST. That's not currently a priority for us at this time, so we have not yet looked into it.

                                • Re: REST API in PowerShell
                                  sw_ross


                                  Thanks, I was able to figure it out.

                                  Powershell API was failing for me with New-SwisObject : Object reference not set to an instance of an object.

                                  But I was able to put the pieces togther based on your script and what I could find in the pdf.

                                  Thanks for your help.

                                   

                                   

                                   

                                   

                                   

                                   

                                  new-swisobject fails

                                    • Re: REST API in PowerShell
                                      triple5_555

                                      Do you have the OrionSDK installed? Have you added the snapin? If so, it's installed as a 32-bit version and we have to open the powershell window as x86 (32-bit) in order to add the snapin.

                                        • Re: REST API in PowerShell
                                          tdanner

                                          The snapin is compatible with both x86 and x64 powershell instances. The installer is supposed to register it in both environments, though we have had some reports of this not happening correctly. To register it manually, run these commands:

                                           

                                          C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe "C:\Program Files (x86)\SolarWinds\Orion SDK\SWQL Studio\SwisPowerShell.dll"

                                          C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtil.exe "C:\Program Files (x86)\SolarWinds\Orion SDK\SWQL Studio\SwisPowerShell.dll"

                                            • Re: REST API in PowerShell
                                              sw_ross

                                              We only have the issue with New-SwisObject.  All the other calls are successful.  Get-SwisData, Get-SwisObject, Remove-SwisObject, Set-SwisObject.  I posted the trace in the other thread.

                                              But at least I now have a workaround using the REST API.  It gets past the hurdle where we were unable to script adding nodes to solarwinds.

                                  • Re: REST API in PowerShell
                                    triple5_555

                                    tdanner,

                                     

                                    I'm have been able to update a single record at a time with REST, similar to:

                                     

                                    $body = @{

                                      UnmanageFrom=$startUnmanage_utc

                                      UnmanageUntil=$endUnmanage_utc

                                      }

                                      $jsonBody = ConvertTo-Json $body

                                     

                                    $updateQuery = "https://"+$orion_server+":17778/SolarWinds/InformationService/v3/Json/$uri"

                                    $results = Invoke-RestMethod -Uri $updateQuery -Credential $orioncred -ContentType "application/json" -Body $jsonBody -Method Post

                                     

                                     

                                    This takes about 6.5 minutes to run on the number of machines we have. We were able to mass update with the SDK, is there a way to bulk update with the REST API? I tried doing multiple $uris in the query. I also tried some queries in the body, but couldn't also update. Do you by chance of example code that updates more than one node/entity at a time with REST? Thanks.

                                      • Re: REST API in PowerShell
                                        tdanner

                                        Unfortunately bulk update is not supported through the rest API at this time.

                                        1 of 1 people found this helpful
                                          • Re: REST API in PowerShell
                                            triple5_555

                                            Thank you for the information. Is this bulk update on the roadmap for future REST API changes? We've seen about 7-14 minute runtimes for one at a time updates (SDK or REST), but bulk updates finish in about 7 seconds. Another thing we've seen is that it takes about 6 seconds to query the database with the SDK and 1 second for the same query with REST.

                                              • Re: REST API in PowerShell
                                                tdanner

                                                Bulk update (and delete) through the REST interface is something I would like to see added, but I can't commit to a timeframe. It's 340423 in our bug tracking system if you want to ask product managers about it.

                                                 

                                                Concerning the time difference in running a query through different channels - that's not something I would expect. Could you be more specific about what you mean by "query the database with the SDK", what the query was, and how much data it returns?

                                    • Re: REST API in PowerShell
                                      Hari Pala

                                      Powershell API was not working for me...Trying to resolved script errors