Public/Test-MacadressApi.ps1
|
function Test-MacadressApi { <# .SYNOPSIS Check that the macadress.com API and its database are reachable. .DESCRIPTION Calls GET /v1/healthz. Keyless, and not counted against any quota. Returns $true or $false; use -Detailed for the status object. .PARAMETER Detailed Return an object with Healthy, StatusCode and BaseUri instead of a bool. .EXAMPLE if (Test-MacadressApi) { 'up' } else { 'down' } .EXAMPLE Test-MacadressApi -Detailed .LINK https://github.com/sapisos/macadress-powershell #> [CmdletBinding()] [OutputType([bool])] param( [Parameter()] [switch] $Detailed ) $healthy = $false $status = $null try { $response = Invoke-MacadressApi -Method GET -Path 'v1/healthz' $healthy = $true $status = 200 if ($response.status -and $response.status -ne 'ok') { $healthy = $false } } catch { $status = $_.Exception.Data['StatusCode'] $healthy = $false } if ($Detailed) { [pscustomobject]@{ PSTypeName = 'Macadress.HealthStatus' Healthy = $healthy StatusCode = $status BaseUri = (Get-MacadressConfigStore).BaseUri } } else { $healthy } } |