Functions/Get-Domains.ps1

function Get-Domains {
    <#
    .SYNOPSIS
        Retrieves domain records from the Gorelo API.
 
    .DESCRIPTION
        - If -ClientId is specified, retrieves that client via /clients/{ClientId} and returns its domains.
        - If no ClientId is specified, retrieves all clients via /clients and flattens all embedded domains.
        - Each returned domain object includes ClientId and ClientName.
        - Optionally filter by Name or ClientName (only one filter can be used at a time).
 
    .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
        (Optional) Retrieve domains for a specific client.
 
    .PARAMETER Name
        (Optional) Filter domains by domain name (supports wildcards).
 
    .PARAMETER ClientName
        (Optional) Filter domains by associated client name (supports wildcards).
 
    .EXAMPLE
        Get-Domains -ApiKey $APIKey # Gets all Domains
 
    .EXAMPLE
        Get-Domains -ApiKey $APIKey -ClientId 'xxx' # Gets Domains by ClientId
 
    .EXAMPLE
        Get-Domains -ApiKey $APIKey -Name '*gorelo.io*' # Gets Domains by Name
 
    .EXAMPLE
        Get-Domains -ApiKey $APIKey -ClientName '*Holdings*' # Gets Domains by ClientName
 
    .NOTES
        Note: There is no /clients/{id}/domains endpoint; domains are returned as a 'domains' property on client objects.
    #>


    [CmdletBinding()]
    param(
        [string]$BaseUrl     = "https://api.usw.gorelo.io",
        [string]$ApiVersion  = "v1",

        [Parameter(Mandatory = $true)]
        [string]$ApiKey,

        [string]$ClientId,

        [string]$Name,

        [string]$ClientName
    )

    # --- Filter enforcement ---
    $filters = @($Name, $ClientName) | Where-Object { $_ }
    if ($filters.Count -gt 1) {
        throw "Please specify only one search filter at a time (Name or ClientName)."
    }

    if (-not $ApiKey) {
        throw "API key not provided. Use -ApiKey"
    }

    $headers = @{
        "X-API-Key"    = $ApiKey
        "Accept"       = "application/json"
        "Content-Type" = "application/json"
    }

    $allDomains = @()

    if ($ClientId) {
        # --- Get single client ---
        $clientUri = "$($BaseUrl.TrimEnd('/'))/$($ApiVersion.Trim('/'))/clients/$ClientId"
        try {
            Write-Verbose "Retrieving client $ClientId from $clientUri"
            $client = Invoke-RestMethod -Uri $clientUri -Method GET -Headers $headers -ErrorAction Stop
        }
        catch {
            throw "Failed to retrieve client $ClientId : $($_.Exception.Message)"
        }

        if ($client.domains) {
            foreach ($d in $client.domains) {
                $d | Add-Member -NotePropertyName ClientId -NotePropertyValue $ClientId -Force
                if ($client.Name) {
                    $d | Add-Member -NotePropertyName ClientName -NotePropertyValue $client.Name -Force
                }
                $allDomains += $d
            }
        }
    }
    else {
        # --- Get all clients ---
        $clientsUri = "$($BaseUrl.TrimEnd('/'))/$($ApiVersion.Trim('/'))/clients"
        try {
            Write-Verbose "Retrieving clients from $clientsUri"
            $clients = Invoke-RestMethod -Uri $clientsUri -Method GET -Headers $headers -ErrorAction Stop
        }
        catch {
            throw "Failed to retrieve clients: $($_.Exception.Message)"
        }

        foreach ($c in $clients) {
            $cid = $c.ID
            if (-not $cid) { continue }

            if ($c.domains) {
                foreach ($d in $c.domains) {
                    $d | Add-Member -NotePropertyName ClientId -NotePropertyValue $cid -Force
                    if ($c.Name) {
                        $d | Add-Member -NotePropertyName ClientName -NotePropertyValue $c.Name -Force
                    }
                    $allDomains += $d
                }
            }
        }
    }

    # --- Apply single filter ---
    switch ($true) {
        { $Name } {
            Write-Verbose "Filtering domains by Name: $Name"
            $allDomains = $allDomains | Where-Object { $_.name -like $Name }
            break
        }
        { $ClientName } {
            Write-Verbose "Filtering domains by ClientName: $ClientName"
            $allDomains = $allDomains | Where-Object { $_.ClientName -like $ClientName }
            break
        }
    }

    return $allDomains
}

<#
 
$APIKey = 'xxx'
 
Get-Domains -ApiKey $APIKey # Gets All Domains
Get-Domains -ApiKey $APIKey -ClientId 'xxx' # Gets Domains by ClientID
Get-Domains -ApiKey $APIKey -Name "xxx" # Gets Domains by Name
Get-Domains -ApiKey $APIKey -ClientName "xxx" # Gets Domains by ClientName
 
#>