Public/TenantConfiguration/Enable-CustomerDKIM.ps1
function Enable-CustomerDKIM() { param( [Parameter(Mandatory)] [string]$TenantId ) Write-Host "[DKIM] Starting DKIM configuration..." -ForegroundColor Yellow try { Write-Host "[DKIM] Connecting to Customer environment.." -ForegroundColor Yellow Connect-CustomerExchange -CustomerTenantId $TenantId Connect-CustomerGraph -CustomerTenantId $TenantId $DomainsWithoutDKIM = @() Get-MgDomain | Where-Object { $_.Id -NotLike '*.onmicrosoft.com' } | ForEach-Object { Write-Host "[$($_.Id)] Checking domain.." -ForegroundColor Yellow $DKIMStatus = Get-DkimSigningConfig -Identity $_.Id -ErrorAction SilentlyContinue if( -not $DKIMStatus -or $DKIMStatus.Enabled -eq $false) { $DomainsWithoutDKIM += [pscustomobject]@{ Domain = $_.Id } } } if ( $DomainsWithoutDKIM.Count -eq 0 ) { Write-Host "[DKIM] No domains without DKIM found." -ForegroundColor Green Read-Host "Press any key..." return } $SelectedDomains = $DomainsWithoutDKIM | Out-ConsoleGridView -Title "Select domains to enable DKIM for.." -OutputMode Multiple $SelectedDomains | ForEach-Object { $Domain = $_ $DomainName = $Domain.Domain Write-Host "[$($DomainName)] Getting DKIM configuration..." -ForegroundColor Yellow $DKIMConfig = Get-DkimSigningConfig -Identity $DomainName -ErrorAction SilentlyContinue if ( -not $DKIMConfig ) { Write-Host "[$($DomainName)] DKIM signing config not found. Trying to enable." -ForegroundColor Yellow New-DkimSigningConfig -DomainName $DomainName -Enabled $true -ErrorAction SilentlyContinue | Out-Null $DKIMConfig = Get-DkimSigningConfig -Identity $DomainName -ErrorAction SilentlyContinue } if ( $DKIMConfig ) { if ( $DKIMConfig.Enabled ) { Write-Host "[$($DomainName)] DKIM signing config is enabled. Skipping..." -ForegroundColor Yellow } else { $cname1 = "selector1._domainkey.$($DomainName)" $cname2 = "selector2._domainkey.$($DomainName)" $CreateManual = $false $cname1Dns = Resolve-DnsName -Name $cname1 -Type CNAME -ErrorAction SilentlyContinue $cname2Dns = Resolve-DnsName -Name $cname2 -Type CNAME -ErrorAction SilentlyContinue $NameServer = Resolve-DnsName -Name $DomainName -Type NS -ErrorAction SilentlyContinue Write-Host "[$($DomainName)] Checking DNS records..." -ForegroundColor Yellow if ( !$cname1Dns.NameHost -and !$cname2Dns.NameHost ) { Write-Host "[$($DomainName)] CNAME records not found." -ForegroundColor Yellow Write-Host "[$($DomainName)] Checking nameservers..." -ForegroundColor Yellow if( $NameServer.NameHost -notlike '*.curanet.dk' ) { Write-Host "[$($DomainName)] Nameservers are not curanet.dk." -ForegroundColor Yellow $CreateManual = $true } else { $DNSRecords = Get-CuraDNSRecords -DomainName $DomainName if ( $DNSRecords.status -ne 404 ) { Write-Host "[$($DomainName)] Domain found on curanet." -ForegroundColor Yellow Write-Host "[$($DomainName)] Trying to create first cname record... " -ForegroundColor Yellow -NoNewline $Cname1Result = New-CuraDNSRecord -DomainName $DomainName -Hostname 'selector1._domainkey' -Type 'CNAME' -Value $($DKIMConfig.Selector1CNAME.Trim()) if ( -not $Cname1Result.status ) { Write-Host "SUCCESS!" -ForegroundColor Green } else { Write-Host "FAILED!" -ForegroundColor Red $CreateManual = $true } Write-Host "[$($DomainName)] Trying to create second cname record... " -ForegroundColor Yellow -NoNewline $Cname2Result = New-CuraDNSRecord -DomainName $DomainName -Hostname 'selector2._domainkey' -Type 'CNAME' -Value $($DKIMConfig.Selector2CNAME.Trim()) if( -not $Cname2Result.status ) { Write-Host "SUCCESS!" -ForegroundColor Green } else { Write-Host "FAILED!" -ForegroundColor Red $CreateManual = $true } } else { Write-Host "[$($DomainName)] Domain not found on curanet." -ForegroundColor Yellow $CreateManual = $true } if ( $CreateManual ) { Write-Host "[$($DomainName)] Please create the following CNAME records manually:" -ForegroundColor Yellow Write-Host "[$($DomainName)] selector1._domainkey.$($DomainName) -> $($DKIMConfig.Selector1CNAME.Trim())" -ForegroundColor Yellow Write-Host "[$($DomainName)] selector2._domainkey.$($DomainName) -> $($DKIMConfig.Selector2CNAME.Trim())" -ForegroundColor Yellow Write-Host "[$($DomainName)] Press any key to continue..." -ForegroundColor Yellow Read-Host } } } else { Write-Host "[$($DomainName)] CNAME records found." -ForegroundColor Yellow } Write-Host "[$($DomainName)] Enabeling DKIM" -ForegroundColor Yellow -NoNewline Set-DkimSigningConfig -Identity $DomainName -Enabled $true -ErrorAction SilentlyContinue | Out-Null While(1) { Write-Host "." -ForegroundColor Yellow -NoNewline $result = Get-DkimSigningConfig -Identity $DomainName -ErrorAction SilentlyContinue if($result.Enabled -eq $true) { Write-Host " Done!" -ForegroundColor Green Read-Host "Press any key..." break } Set-DkimSigningConfig -Identity $DomainName -Enabled $true -ErrorAction SilentlyContinue | Out-Null Start-Sleep -Seconds 5 } } } else { Write-Host "[$($DomainName)] Could not enable DKIM Signing config. -Maybe exchange has not propagated yet on the tenant!" -ForegroundColor Red Read-Host "Press any key..." } } } catch { throw "Failed to enable DKIM: $_" } } |