Requesting Custom Asset Report through PowerShell
I'm trying to use Invoke-WebRequest in PowerShell to request all devices Serial, Location, Department, and Status in the 'Chromebook' category so I can filter it and move the devices with GAM to the correct OU in Google Admin.
I am able to make a request but i'm getting the 'Sad Panda, not authorized" error.
How is SnipeIT expecting the credentials? If you need any more information I'll be happy to provide it, I've been trying to figure it out for weeks.
You'd need to provide the Bearer token with your request. https://snipe-it.readme.io/reference#authenticating-with-the-api
Snipe-IT DocumentationSnipe-IT is a free, open source IT asset management system. Features include management of assets, users, licenses, accessories, consumables and components, as well as two-factor authentication, LDAP/AD syncing, and asset acceptance confirmation.
Here's a more complete example using Invoke-RestMethod, which is very similar to Invoke-WebRequest with the extra steps to parse the JSON baked in:
$Token = "<Your-Snipe-IT-API-Token-From-The-Above-Link>"
$APIUri = "http://your-instance-host/api/v1"
$Parameters = @{ # This is a common set of parameters to be used for all requests, including the authorization header
UseBasicParsing = $true
Headers = @{
Authorization = "Bearer $Token" # this is the important one
'Content-Type' = 'application/json'
}
}
# The number of assets returned per call is limited, so we need to get the total and the first batch
$FirstSet = Invoke-RestMethod @Parameters "$APIUri/hardware"
$Retrieved = $FirstSet.rows.Count
$AllAssets = @(
$FirstSet.Rows # The first set of responses
while($Retrieved -lt $FirstSet.total){
$Batch = Invoke-RestMethod @Parameters "$APIUri/hardware?offset=$Retrieved"
$Retrieved += $Batch.rows.Count
$Batch.rows # each subsequent set
}
)
$AllAssets will be an array of all assets in your environment, with every property they have. You'll need to use calculated properties on Select-Object if you want a flat table.
I got it. Thanks all!