Public/Select-PDAccount.ps1
|
function Select-PDAccount { <# .SYNOPSIS Sets the default MSSP customer account for the current PowerDMARC session. .DESCRIPTION Updates the AccountId stored by Connect-PowerDMARC so that subsequent calls to Reseller-mode cmdlets (e.g. Get-PDDomain, Get-PDHostedSPF) that need an AccountId don't have to pass -AccountId every time. Only meaningful in Reseller mode. By default the account is verified with a Get-PDAccount call before being selected, so a mistyped or inaccessible AccountId fails immediately rather than on the next unrelated call. Use -SkipValidation to select without that check. .PARAMETER AccountId The MSSP account ID to make the default for this session. Accepts pipeline input, including directly from Get-PDAccount. .PARAMETER SkipValidation Set the AccountId without first verifying it via the API. .PARAMETER PassThru Return the selected account object (or, with -SkipValidation, the AccountId) instead of producing no output. .EXAMPLE Select-PDAccount -AccountId 42 .EXAMPLE Get-PDAccount | Where-Object name -eq 'Contoso' | Select-PDAccount .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', 'account_id')] [int]$AccountId, [Parameter()] [switch]$SkipValidation, [Parameter()] [switch]$PassThru ) process { $ctx = Get-PDRequestContext if ($ctx.Mode -ne 'Reseller') { throw 'Select-PDAccount is only available in Reseller mode. Reconnect with Connect-PowerDMARC -Mode Reseller.' } $account = $null if (-not $SkipValidation) { $account = Get-PDAccount -AccountId $AccountId -ErrorAction Stop } $script:PDConnection.AccountId = $AccountId $accountLabel = if ($account -and $account.name) { "$AccountId ($($account.name))" } else { "$AccountId" } Write-Host "PowerDMARC: default MSSP account set to $accountLabel" -ForegroundColor Green if ($PassThru) { if ($account) { $account } else { $AccountId } } } } |