Public/Get-PDAccount.ps1

function Get-PDAccount {
    <#
    .SYNOPSIS
        Retrieves PowerDMARC MSSP customer account(s).

    .DESCRIPTION
        Calls the PowerDMARC MSSP Account Management API (relative to the /mssp root stored by
        Connect-PowerDMARC -Mode Reseller):
          - GET /accounts/{accountId} for a specific account.
          - GET /accounts?dateFrom=...&dateTo=... to list customer accounts created within a date
            range. The API requires both dateFrom and dateTo on the list call; if omitted here,
            they default to the last 30 days.

        Requires an active connection created with Connect-PowerDMARC -Mode Reseller. Accounts
        are a Reseller-only concept; this cmdlet is not available in Default mode.

    .PARAMETER AccountId
        The MSSP account ID(s) to retrieve.

    .PARAMETER DateFrom
        Start of the date range to list accounts for. Defaults to 30 days ago.

    .PARAMETER DateTo
        End of the date range to list accounts for. Defaults to today.

    .EXAMPLE
        Get-PDAccount
        # Lists accounts from the last 30 days.

    .EXAMPLE
        Get-PDAccount -DateFrom '2026-01-01' -DateTo '2026-12-31'

    .EXAMPLE
        Get-PDAccount -AccountId 42

    .EXAMPLE
        Get-PDAccount -DateFrom (Get-Date).AddMonths(-1) -DateTo (Get-Date) | Select-PDAccount

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

    [CmdletBinding(DefaultParameterSetName = 'List')]
    [OutputType([pscustomobject])]
    param(
        [Parameter(ParameterSetName = 'ById', Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [int[]]$AccountId,

        [Parameter(ParameterSetName = 'List')]
        [datetime]$DateFrom = (Get-Date).AddDays(-30),

        [Parameter(ParameterSetName = 'List')]
        [datetime]$DateTo = (Get-Date)
    )

    begin {
        $ctx = Get-PDRequestContext

        if ($ctx.Mode -ne 'Reseller') {
            throw 'Get-PDAccount is only available in Reseller mode. Reconnect with Connect-PowerDMARC -Mode Reseller.'
        }
    }

    process {
        function Format-PDAccount {
            param([Parameter(ValueFromPipeline)][object]$Account)
            process {
                if ($null -eq $Account) { return }
                foreach ($user in $Account.users) { $null = Set-PDTypeName -InputObject $user -TypeName 'PowerDMARC.User' }
                foreach ($domain in $Account.domains) { $null = Set-PDTypeName -InputObject $domain -TypeName 'PowerDMARC.Domain' }
                $null = Set-PDTypeName -InputObject $Account.plan -TypeName 'PowerDMARC.Plan'
                Set-PDTypeName -InputObject $Account -TypeName 'PowerDMARC.Account'
            }
        }

        if ($PSCmdlet.ParameterSetName -eq 'ById') {
            foreach ($id in $AccountId) {
                $uri = "$($ctx.BaseUri)/accounts/$id"

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

                $accountResult = if ($null -ne $response.data) { $response.data } else { $response }
                $accountResult | Format-PDAccount
            }
        } else {
            $query = 'dateFrom={0}&dateTo={1}' -f
                [System.Uri]::EscapeDataString($DateFrom.ToString('yyyy-MM-dd')),
                [System.Uri]::EscapeDataString($DateTo.ToString('yyyy-MM-dd'))
            $uri = "$($ctx.BaseUri)/accounts?$query"

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

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