<# .SYNOPSIS Prepares and turns on DKIM signing in Exchange Online for every domain you send from. .DESCRIPTION Mail is signed by Innovarro Signatures and then handed back to Exchange Online, which DKIM-signs it on the way out. Signing with your own domain (instead of the default .onmicrosoft.com) lets DMARC pass on DKIM as well as SPF, which survives forwarding and is what Gmail, Yahoo and Microsoft expect from bulk and business senders. Run 1 (no switches): creates 2048-bit DKIM keys where missing and lists the two CNAME records per domain that you must add at your DNS provider. Run 2 (-Enable), after the CNAMEs are published: checks the CNAMEs and turns DKIM signing on. .PARAMETER Domain Only these domains. Default: every authoritative accepted domain except *.onmicrosoft.com. .PARAMETER Enable Turn DKIM signing on for domains whose CNAME records are published. .EXAMPLE .\Enable-InnovarroDkim.ps1 # list the CNAMEs to publish .\Enable-InnovarroDkim.ps1 -Enable # after publishing them .NOTES Innovarro Signatures - https://signatures.innovarro.cloud/support #> [CmdletBinding(SupportsShouldProcess)] param([string[]]$Domain, [switch]$Enable) $ErrorActionPreference = 'Stop' if (-not (Get-Command Get-ConnectionInformation -ErrorAction SilentlyContinue)) { throw 'The ExchangeOnlineManagement module is not installed. Run: Install-Module ExchangeOnlineManagement -Scope CurrentUser' } if (-not (Get-ConnectionInformation | Where-Object State -eq 'Connected')) { Connect-ExchangeOnline -ShowBanner:$false } function Get-Cname([string]$name) { # Resolve-DnsName exists on Windows only; elsewhere fall back to nslookup output. try { if (Get-Command Resolve-DnsName -ErrorAction SilentlyContinue) { return (Resolve-DnsName -Name $name -Type CNAME -DnsOnly -ErrorAction Stop | Where-Object Type -eq 'CNAME' | Select-Object -First 1).NameHost } $out = nslookup -type=CNAME $name 2>$null | Select-String 'canonical name = (.+)$' if ($out) { return $out.Matches[0].Groups[1].Value } } catch { } return $null } if (-not $Domain) { $Domain = Get-AcceptedDomain | Where-Object { $_.DomainType -eq 'Authoritative' -and $_.DomainName -notlike '*.onmicrosoft.com' } | ForEach-Object { $_.DomainName.ToString() } } $results = foreach ($d in $Domain) { $cfg = Get-DkimSigningConfig -Identity $d -ErrorAction SilentlyContinue if (-not $cfg -and $PSCmdlet.ShouldProcess($d, 'Create DKIM keys (2048-bit)')) { $cfg = New-DkimSigningConfig -DomainName $d -KeySize 2048 -Enabled $false } if (-not $cfg) { continue } $s1 = Get-Cname "selector1._domainkey.$d" $s2 = Get-Cname "selector2._domainkey.$d" $published = ($s1 -and $s1.TrimEnd('.') -eq $cfg.Selector1CNAME.TrimEnd('.')) -and ($s2 -and $s2.TrimEnd('.') -eq $cfg.Selector2CNAME.TrimEnd('.')) $state = if ($cfg.Enabled) { 'Signing' } elseif ($published) { 'Ready to enable' } else { 'Publish CNAMEs' } if ($Enable -and -not $cfg.Enabled) { if (-not $published) { Write-Warning "$d : CNAME records not found (yet). DNS changes can take up to an hour to show." } elseif ($PSCmdlet.ShouldProcess($d, 'Enable DKIM signing')) { try { Set-DkimSigningConfig -Identity $d -Enabled $true; $state = 'Signing (just enabled)' } catch { Write-Warning "$d : $($_.Exception.Message)"; $state = 'Enable failed - retry in a few minutes' } } } [pscustomobject]@{ Domain = $d; Status = $state; Host1 = "selector1._domainkey"; Target1 = $cfg.Selector1CNAME; Host2 = "selector2._domainkey"; Target2 = $cfg.Selector2CNAME } } $results | Format-Table Domain, Status -AutoSize $todo = $results | Where-Object Status -eq 'Publish CNAMEs' if ($todo) { Write-Host 'Add these CNAME records at your DNS provider (record name is relative to the domain):' -ForegroundColor Cyan foreach ($r in $todo) { Write-Host "`n $($r.Domain)" Write-Host " CNAME $($r.Host1) -> $($r.Target1)" Write-Host " CNAME $($r.Host2) -> $($r.Target2)" } Write-Host "`nThen run: .\Enable-InnovarroDkim.ps1 -Enable" }