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). Ignored when -All is used. .PARAMETER All Reseller mode only. Ignores AccountId/the selected account and instead lists every customer account ever created (via Get-PDAccount, spanning its full history rather than its default 30-day window) and every domain under each of them. PowerDMARC's MSSP API has no single account-less "list all domains" endpoint, so this makes one /accounts/{accountId}/domains call per account to cover the whole reseller. .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. .EXAMPLE Get-PDDomain -All # Same result as the example above, without fetching accounts yourself first. .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, [Parameter()] [switch]$All ) begin { $ctx = Get-PDRequestContext } process { if ($All) { if ($ctx.Mode -ne 'Reseller') { throw '-All is only valid in Reseller mode.' } foreach ($account in (Get-PDAccount -DateFrom ([datetime]'2000-01-01') -DateTo (Get-Date))) { if ($PSBoundParameters.ContainsKey('DomainId')) { Get-PDDomain -AccountId $account.id -DomainId $DomainId } else { Get-PDDomain -AccountId $account.id } } return } 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 } } } |