Functions/Get-ClientOverview.ps1
|
function Get-ClientOverview { <# .SYNOPSIS Retrieves a complete overview of a specific client from the Gorelo API. .DESCRIPTION Combines related data for a single client, including: - Client information - Assets - Locations - Domains - Contacts (Invoices are intentionally excluded.) If -ClientName is specified, it will resolve the ClientId via Get-Clients automatically. .PARAMETER BaseUrl Base API URL (default: https://api.usw.gorelo.io) .PARAMETER ApiVersion API version (default: v1) .PARAMETER ApiKey API key for authentication. .PARAMETER ClientId The unique ID of the client to retrieve information for. .PARAMETER ClientName The display name of the client to look up and retrieve information for. .EXAMPLE Get-ClientOverview -ApiKey $APIKey -ClientId 'xxx' # Retrieves by ClientId .EXAMPLE Get-ClientOverview -ApiKey $APIKey -ClientName 'Acme Holdings' # Retrieves by ClientName #> [CmdletBinding()] param( [string]$BaseUrl = "https://api.usw.gorelo.io", [string]$ApiVersion = "v1", [Parameter(Mandatory = $true)] [string]$ApiKey, [string]$ClientId, [string]$ClientName ) if (-not $ApiKey) { throw "API key not provided. Use -ApiKey" } if (-not $ClientId -and -not $ClientName) { throw "Please specify either -ClientId or -ClientName." } # Resolve ClientId from ClientName if provided if ($ClientName) { Write-Verbose "Resolving ClientName '$ClientName' to ClientId via Get-Clients" $clientMatch = Get-Clients -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -Name $ClientName -ErrorAction Stop if (-not $clientMatch) { throw "No client found matching name '$ClientName'." } if ($clientMatch.Count -gt 1) { Write-Warning "Multiple clients found matching '$ClientName'. Using the first result: $($clientMatch[0].Name) ($($clientMatch[0].ID))" } $ClientId = $clientMatch[0].ID } # Retrieve client details try { $client = Get-Clients -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -ID $ClientId -ErrorAction Stop } catch { throw "Failed to retrieve client $ClientId : $($_.Exception.Message)" } # Retrieve related entities try { $assets = Get-Assets -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -ClientID $ClientId -ErrorAction SilentlyContinue $locations = Get-Locations -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -ClientId $ClientId -ErrorAction SilentlyContinue $domains = Get-Domains -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -ClientId $ClientId -ErrorAction SilentlyContinue $contacts = Get-Contacts -ApiKey $ApiKey -BaseUrl $BaseUrl -ApiVersion $ApiVersion -ClientId $ClientId -ErrorAction SilentlyContinue } catch { Write-Warning "Some related data could not be retrieved: $($_.Exception.Message)" } # Build structured overview object $overview = [PSCustomObject]@{ Client = $client Assets = if ($assets) { $assets } else { @() } Locations = if ($locations) { $locations } else { @() } Domains = if ($domains) { $domains } else { @() } Contacts = if ($contacts) { $contacts } else { @() } Retrieved = (Get-Date) } return $overview } <# $APIKey = 'xxx' Get-ClientOverview -ApiKey $APIKey -ClientId "xxx" # Gets AllClientInfo by ClientID Get-ClientOverview -ApiKey $APIKey -ClientName "xxx" # Gets AllClientInfo by ClientID #> |