This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

REST API in PowerShell

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 IPAddress:17778/.../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.

  • 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?

  • 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!

  • 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?

  • 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

    }

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

  • 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?

  • 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.


  • 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

  • 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.

  • 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"

  • 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.