Functions/Get-Assets.ps1
|
function Get-Assets { <# .SYNOPSIS Retrieves all assets or a specific asset via the API. .DESCRIPTION - If nothing is specified, retrieves all assets from /assets/agents - If -ID is specified, retrieves a single asset from /assets/agents/{ID} - Otherwise, retrieves all assets and optionally filters by ClientID, Name, SerialNo, or LastLoggedOnUser (client-side). - Only one filter is allowed at a time (ID, ClientID, Name, SerialNo, or LastLoggedOnUser) .PARAMETER BaseUrl Base API URL (default: https://api.usw.gorelo.io) .PARAMETER ApiVersion API version (default: v1) .PARAMETER ApiKey API key for authentication. .PARAMETER ID Retrieve a specific asset by ID (e.g., /assets/agents/{ID}) .PARAMETER ClientID Filter assets by client ID. .PARAMETER Name Filter assets by name (client-side). .PARAMETER SerialNo Filter assets by serial number (client-side). .PARAMETER LastLoggedOnUser Filter assets by last logged-on username (client-side). .EXAMPLE Get-Assets -ApiKey "12345" .EXAMPLE Get-Assets -ApiKey "12345" -ID "abc123" .EXAMPLE Get-Assets -ApiKey "12345" -ClientID "7890" .EXAMPLE Get-Assets -ApiKey "12345" -Name "Server01*" .EXAMPLE Get-Assets -ApiKey "12345" SerialNo "PC34*" .EXAMPLE Get-Assets -ApiKey "12345" LastLoggedOnUser "*Gary*" #> [CmdletBinding()] param( [string]$BaseUrl = "https://api.usw.gorelo.io", [string]$ApiVersion = "v1", [Parameter(Mandatory = $true)] [string]$ApiKey, [string]$ID, [string]$ClientID, [string]$Name, [string]$SerialNo, [string]$LastLoggedOnUser ) # Count how many filters were supplied $filterCount = 0 foreach ($f in @($ID, $ClientID, $Name, $SerialNo, $LastLoggedOnUser)) { if ($f) { $filterCount++ } } Write-Verbose "Active filters: $filterCount" if ($filterCount -gt 1) { throw "Please specify only one search filter at a time (ID, ClientID, Name, SerialNo, or LastLoggedOnUser)." } if (-not $ApiKey) { throw "API key not provided. Use -ApiKey" } # Build request URI if ($ID) { $uri = "$($BaseUrl.TrimEnd('/'))/$($ApiVersion.Trim('/'))/assets/agents/$ID" } else { $uri = "$($BaseUrl.TrimEnd('/'))/$($ApiVersion.Trim('/'))/assets/agents" } $headers = @{ "X-API-Key" = $ApiKey "Accept" = "application/json" "Content-Type" = "application/json" } try { Write-Verbose "Sending GET request to $uri" $response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers -ErrorAction Stop } catch { $err = $_ $response = $err.Exception.Response if ($response) { $statusCode = $response.StatusCode.value__ $statusDesc = $response.StatusDescription Write-Warning "API call failed: $statusCode ($statusDesc)" } else { Write-Warning "API call failed: $($err.Exception.Message)" } throw } # Confirm no ID and response received before filtering if (-not $ID -and $response) { $results = $response switch ($true) { { $ClientID } { $results = $results | Where-Object { $_.ClientID -like $ClientID }; break } { $Name } { $results = $results | Where-Object { $_.Name -like $Name }; break } { $SerialNo } { $results = $results | Where-Object { $_.SerialNo -like $SerialNo }; break } { $LastLoggedOnUser} { $results = $results | Where-Object { $_.LastLoggedOnUser -like $LastLoggedOnUser }; break } } return $results }else { return $response } } <# $APIKey = 'xxx' Get-Assets -ApiKey $APIKey # Gets All Assets Get-Assets -ApiKey $APIKey -LastLoggedOnUser 'xxx' # Gets Assets by last logged in user Get-Assets -ApiKey $APIKey -ID 'xxx' # Gets Assets by ID Get-Assets -ApiKey $APIKey -Name 'xxx' # Gets Assets by Name Get-Assets -ApiKey $APIKey -SerialNo 'xxx' # Gets Assets by SerialNo Get-Assets -ApiKey $APIKey -ClientID 'xxx' # Gets Assets in Client by ClientID #> |