Public/Select-PDDomain.ps1

function Select-PDDomain {
    <#
    .SYNOPSIS
        Sets the default domain for the current PowerDMARC session.

    .DESCRIPTION
        Updates the DomainId stored by Connect-PowerDMARC so that subsequent calls to cmdlets
        that need a DomainId (currently Get-PDHostedSPF) don't have to pass -DomainId every time.
        Works in both Default and Reseller mode.

        By default the domain is verified with a Get-PDDomain call before being selected (in
        Reseller mode this uses the already-selected AccountId, so select an account first
        there), so a mistyped or inaccessible DomainId fails immediately rather than on the next
        unrelated call. Use -SkipValidation to select without that check.

    .PARAMETER DomainId
        The domain ID to make the default for this session. Accepts pipeline input, including
        directly from Get-PDDomain.

    .PARAMETER SkipValidation
        Set the DomainId without first verifying it via the API.

    .PARAMETER PassThru
        Return the selected domain object (or, with -SkipValidation, the DomainId) instead of
        producing no output.

    .EXAMPLE
        Select-PDDomain -DomainId 158463

    .EXAMPLE
        Get-PDDomain | Where-Object domain -eq 'meiertobler.ch' | Select-PDDomain

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

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

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '',
        Justification = 'Intentional always-visible confirmation, matching Connect-ExchangeOnline/Connect-MgGraph style status messages; Write-Information would be silent by default.')]
    [CmdletBinding()]
    [OutputType([pscustomobject], [int])]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [int]$DomainId,

        [Parameter()]
        [switch]$SkipValidation,

        [Parameter()]
        [switch]$PassThru
    )

    process {
        $null = Get-PDRequestContext

        $domain = $null
        if (-not $SkipValidation) {
            $domain = Get-PDDomain -DomainId $DomainId -ErrorAction Stop
        }

        $script:PDConnection.DomainId = $DomainId

        $domainLabel = if ($domain -and $domain.name) { "$DomainId ($($domain.name))" } else { "$DomainId" }
        Write-Host "PowerDMARC: default domain set to $domainLabel" -ForegroundColor Green

        if ($PassThru) {
            if ($domain) { $domain } else { $DomainId }
        }
    }
}