SHELL/2.1.9.ps1

$CheckId = "2.1.9"
$Title = "Ensure that DKIM is enabled for all Exchange Online Domains"

try {
    $DkimConfigs = Get-DkimSigningConfig | Select-Object Name,Enabled,Status
    $NonCompliant = $DkimConfigs | Where-Object { $_.Enabled -ne $true -or $_.Status -ne "Valid" }
    $Pass = @($DkimConfigs).Count -gt 0 -and @($NonCompliant).Count -eq 0

    [pscustomobject]@{
        CheckId   = $CheckId
        Title     = $Title
        Status    = if ($Pass) { "PASS" } else { "FAIL" }
        Pass      = $Pass
        Evidence  = [pscustomobject]@{
            DkimConfigs  = @($DkimConfigs)
            NonCompliant = @($NonCompliant)
        }
        Error     = $null
        Timestamp = Get-Date
    }
}
catch {
    [pscustomobject]@{
        CheckId   = $CheckId
        Title     = $Title
        Status    = "ERROR"
        Pass      = $null
        Evidence  = $null
        Error     = $_.Exception.Message
        Timestamp = Get-Date
    }
}