snipe-it icon indicating copy to clipboard operation
snipe-it copied to clipboard

Requesting Custom Asset Report through PowerShell

Open loganhawker opened this issue 6 years ago • 3 comments

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.

loganhawker avatar Sep 12 '20 04:09 loganhawker

You'd need to provide the Bearer token with your request. https://snipe-it.readme.io/reference#authenticating-with-the-api

Snipe-IT Documentation
Snipe-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.

snipe avatar Sep 12 '20 15:09 snipe

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.

cofl avatar Oct 01 '20 19:10 cofl

I got it. Thanks all!

loganhawker avatar Oct 06 '20 18:10 loganhawker