Powershell API Call Filtering Assets

Hello,

I am working on automating our employee offboarding process and one of the items that would save me a good chunk of time is using the API to get all of the assets associated with a particular user.

I am currently stuck on the formatting of the API call using the PowerShell snippet below.

$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("MY_API_KEY"))}

$id = 11000998613
$user_id = "user_id:$id"

$user_asset = invoke-RestMethod -URI https://novinium.freshservice.com/api/v2/assets?include=type_fields'&'query=$user_id -Headers $Header -ContentType "application/json" -Method Get

I am receiving back:
Error: (400) bad request
message=Given query is invalid, expected format “keyword:value OPERATOR keyword:‘string’ OPERATOR keyword:>‘yyyy-mm-dd’ OPERATOR keyword:<integer”. Space is mandatory between key/value pair and operator. Please check the paranthesis if there are any.

When I use Fiddler to exam the API call I see that my Request Headers contains: GET /api/v2/assets?include=type_fields&query=user_id:11000998613 HTTP/1.1

Any assistance with this would be greatly appreciated!

Look at how the documentation formats the value of the Query Parameter. It is wrapped in double quotes.

&query="asset_state:%27IN%20STOCK%27%20AND%20created_at:>%272018-08-10%27"

It is probably easier to write your URI like this:

"https://novinium.freshservice.com/api/v2/assets?include=type_fields&query=""$user_id"""

Wrap your entire URI in double quotes, then the the Query parameter requires the query expression be wrapped in double quotes. You will need to escape them using double double quotes.

Also FTR If you use powershell 7 you don’t have to build the header. Powershell will convert it from the values of the -Credential parameter automatically.

Thank you so much for that! This has been something I’ve been working on slowly in between my other work and I knew there had to be something simple I was missing.

I really appreciate your assistance with this and will go and check on that -Credential parameter right now.

1 Like