Powershell & API hooks failing

Trying to use powershell to pull pull information from samanage but I keep getting an error when just testing the authentication part documented here

Below is the code I am running with my token removed.

$token = 'TOKEN' 
$apiver = 'Accept: application/vnd.samanage.v2.1+json' 
$apiurl = 'https://api.samanage.com/' 

curl -H 'X-Samanage-Authorization: Bearer $token' -H $apiver -H 'Content-Type: application/json' -X GET $apiurl + incidents.json

 And then here is the error I get:

image.png

EDIT: I did some google searching for the error and everything i found basically said to do it differently but none of it was related to SWSD.

  • Give this a try instead:

    $token = 'TOKEN'
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" 
    $headers.Add("Accept", 'application/vnd.samanage.v2.1+xml')
    $headers.Add("X-Samanage-Authorization", "Bearer $token")
    $apiurl = 'https://api.samanage.com/' 
    
    curl -H $headers -X GET $apiurl + incidents.json
  • A couple quick notes on what ztrikker provided (which by the way after the tweaks and suggestions I am going to add works)

    First curl is just an alias for invoke-RestMethod and given that powershell v 7 has native support for curl as it is intended to be xPlatform with linux and apple it would be best practice IMO to use the full cmdlet if only so that later versions of powershell don't booger up your script or have it firing unusually.

    Second for what ztrikker suggested to work you need to drop the -X as it will not be recognized as a valid parameter of invoke-restmethod instead use   -Method 

    Finally I would drop the '/' at the end of $apiurl as the '+' doesn't concatenate the strings in this case instead you can just append the '/' with the targeted endpoint text in this case /incidents.json

    My working script uses the following:

    $token = 'TOKEN'
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Accept", 'application/vnd.samanage.v2.1+json')
    $headers.Add("X-Samanage-Authorization", "Bearer $token")
    $apiurl = 'https://api.samanage.com'

    invoke-restmethod -Headers $headers -Method GET $apiurl/incidents.json

    Update:

    If you want to perform POST requests you need to include -body for your input xml and -contentType "text/xml" or "text/json" where applicable


  • I was able to integrate with the SolarWinds Service Desk API using Invoke-RestMethod in PowerShell without too much difficulty.