Public/Get-PDDomain.ps1

function Get-PDDomain {
    <#
    .SYNOPSIS
        Retrieves PowerDMARC domain(s).

    .DESCRIPTION
        Calls the PowerDMARC Domain Management API. Requires an active connection created with
        Connect-PowerDMARC.

        Behavior depends on the mode (and root URI) stored by Connect-PowerDMARC:
          - Default mode (root /api/v1): GET /domains/{domainId} for a specific domain, or
            GET /domains to list every domain on the token's own account.
          - Reseller mode (root /api/v1/mssp): GET /accounts/{accountId}/domains/{domainId} for a
            specific domain, or GET /accounts/{accountId}/domains to list every domain under the
            given MSSP customer account. AccountId is required in this mode.

    .PARAMETER DomainId
        The PowerDMARC domain ID(s) to retrieve. Omit to list every domain in scope.

    .PARAMETER AccountId
        Reseller mode only. The MSSP customer account ID that owns the domain(s). Defaults to the
        account selected via Select-PDAccount (or set with Connect-PowerDMARC -AccountId), if
        any. Accepts pipeline input by property name, including directly from Get-PDAccount (its
        "id" property).

    .EXAMPLE
        Get-PDDomain -DomainId 12345

    .EXAMPLE
        Get-PDDomain

    .EXAMPLE
        Get-PDDomain -AccountId 42 -DomainId 12345

    .EXAMPLE
        Get-PDAccount | Get-PDDomain
        # Lists domains for every account returned, using each account's id as -AccountId.

    .LINK
        https://api.powerdmarc.com/api/end-user/api-v-1-documentation

    .LINK
        https://api.powerdmarc.com/api/mssp/api-v-1-documentation
    #>

    [CmdletBinding()]
    [OutputType([pscustomobject])]
    param(
        [Parameter(Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [int[]]$DomainId,

        [Parameter(ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [int]$AccountId
    )

    begin {
        $ctx = Get-PDRequestContext
    }

    process {
        if ($ctx.Mode -eq 'Reseller') {
            if (-not $PSBoundParameters.ContainsKey('AccountId')) {
                $AccountId = $ctx.AccountId
            }
            if (-not $AccountId) {
                throw 'Reseller mode requires an AccountId. Pass -AccountId, pipe an object with an id/AccountId property (e.g. from Get-PDAccount), or set a default with Select-PDAccount (or Connect-PowerDMARC -AccountId).'
            }
        }

        function Format-PDDomain {
            param([Parameter(ValueFromPipeline)][object]$Domain)
            process {
                if ($null -eq $Domain) { return }
                $null = Set-PDTypeName -InputObject $Domain.owner -TypeName 'PowerDMARC.User'
                Set-PDTypeName -InputObject $Domain -TypeName 'PowerDMARC.Domain'
            }
        }

        if ($DomainId) {
            foreach ($id in $DomainId) {
                $uri = if ($ctx.Mode -eq 'Reseller') {
                    "$($ctx.BaseUri)/accounts/$AccountId/domains/$id"
                } else {
                    "$($ctx.BaseUri)/domains/$id"
                }

                try {
                    $response = Invoke-RestMethod -Uri $uri -Headers $ctx.Headers -Method Get -ErrorAction Stop
                } catch {
                    Write-Error "Failed to retrieve PowerDMARC domain ID $id`: $($_.Exception.Message)"
                    continue
                }

                $domainResult = if ($null -ne $response.data) { $response.data } else { $response }
                $domainResult | Format-PDDomain
            }
        } else {
            $uri = if ($ctx.Mode -eq 'Reseller') {
                "$($ctx.BaseUri)/accounts/$AccountId/domains"
            } else {
                "$($ctx.BaseUri)/domains"
            }

            try {
                $response = Invoke-RestMethod -Uri $uri -Headers $ctx.Headers -Method Get -ErrorAction Stop
            } catch {
                Write-Error "Failed to list PowerDMARC domains: $($_.Exception.Message)"
                return
            }

            $domainResult = if ($null -ne $response.data) { $response.data } else { $response }
            $domainResult | Format-PDDomain
        }
    }
}