Skip to content

Commit be94a7f

Browse files
Merge pull request KelvinTegelaar#1393 from kris6673/fix-dkim-standard
Fix: DKIM standard enabling issue
2 parents 1d223eb + 2633df4 commit be94a7f

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardAddDKIM.ps1

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,41 @@ function Invoke-CIPPStandardAddDKIM {
3434
#$Rerun -Type Standard -Tenant $Tenant -API 'AddDKIM' -Settings $Settings
3535

3636

37-
$AllDomains = (New-GraphGetRequest -uri 'https://graph.microsoft.com/v1.0/domains?$top=999' -tenantid $Tenant | Where-Object { $_.supportedServices -contains 'Email' -or $_.id -like '*mail.onmicrosoft.com' }).id
38-
$DKIM = (New-ExoRequest -tenantid $tenant -cmdlet 'Get-DkimSigningConfig') | Select-Object Domain, Enabled, Status
37+
$DkimRequest = @(
38+
@{
39+
CmdletInput = @{
40+
CmdletName = 'Get-AcceptedDomain'
41+
Parameters = @{}
42+
}
43+
},
44+
@{
45+
CmdletInput = @{
46+
CmdletName = 'Get-DkimSigningConfig'
47+
Parameters = @{}
48+
}
49+
}
50+
)
51+
52+
$BatchResults = New-ExoBulkRequest -tenantid $Tenant -cmdletArray $DkimRequest -useSystemMailbox $true
53+
54+
# Check for errors in the batch results. Cannot continue if there are errors.
55+
$ErrorCounter = 0
56+
$ErrorMessages = [System.Collections.Generic.List[string]]::new()
57+
$BatchResults | ForEach-Object {
58+
if ($_.error) {
59+
$ErrorCounter++
60+
$ErrorMessage = Get-NormalizedError -Message $_.error
61+
$ErrorMessages.Add($ErrorMessage)
62+
}
63+
}
64+
if ($ErrorCounter -gt 0) {
65+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to get DKIM config. Error: $($ErrorMessages -join ', ')" -sev Error
66+
return
67+
}
68+
69+
70+
$AllDomains = ($BatchResults | Where-Object { $_.DomainName }).DomainName
71+
$DKIM = $BatchResults | Where-Object { $_.Domain } | Select-Object Domain, Enabled, Status
3972

4073
# List of domains for each way to enable DKIM
4174
$NewDomains = $AllDomains | Where-Object { $DKIM.Domain -notcontains $_ }
@@ -44,10 +77,10 @@ function Invoke-CIPPStandardAddDKIM {
4477
If ($Settings.remediate -eq $true) {
4578

4679
if ($null -eq $NewDomains -and $null -eq $SetDomains) {
47-
Write-LogMessage -API 'Standards' -tenant $tenant -message 'DKIM is already enabled for all available domains.' -sev Info
80+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'DKIM is already enabled for all available domains.' -sev Info
4881
} else {
4982
$ErrorCounter = 0
50-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Trying to enable DKIM for:$($NewDomains -join ', ' ) $($SetDomains.Domain -join ', ')" -sev Info
83+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Trying to enable DKIM for:$($NewDomains -join ', ' ) $($SetDomains.Domain -join ', ')" -sev Info
5184

5285
# New-domains
5386
$Request = $NewDomains | ForEach-Object {
@@ -58,12 +91,12 @@ function Invoke-CIPPStandardAddDKIM {
5891
}
5992
}
6093
}
61-
if ($null -ne $Request) { $BatchResults = New-ExoBulkRequest -tenantid $tenant -cmdletArray @($Request) -useSystemMailbox $true }
94+
if ($null -ne $Request) { $BatchResults = New-ExoBulkRequest -tenantid $Tenant -cmdletArray @($Request) -useSystemMailbox $true }
6295
$BatchResults | ForEach-Object {
6396
if ($_.error) {
6497
$ErrorCounter ++
6598
$ErrorMessage = Get-NormalizedError -Message $_.error
66-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to enable DKIM. Error: $ErrorMessage" -sev Error
99+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to enable DKIM. Error: $ErrorMessage" -sev Error
67100
}
68101
}
69102

@@ -76,21 +109,21 @@ function Invoke-CIPPStandardAddDKIM {
76109
}
77110
}
78111
}
79-
if ($null -ne $Request) { $BatchResults = New-ExoBulkRequest -tenantid $tenant -cmdletArray @($Request) -useSystemMailbox $true }
112+
if ($null -ne $Request) { $BatchResults = New-ExoBulkRequest -tenantid $Tenant -cmdletArray @($Request) -useSystemMailbox $true }
80113
$BatchResults | ForEach-Object {
81114
if ($_.error) {
82115
$ErrorCounter ++
83116
$ErrorMessage = Get-NormalizedError -Message $_.error
84-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to set DKIM. Error: $ErrorMessage" -sev Error
117+
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to set DKIM. Error: $ErrorMessage" -sev Error
85118
}
86119
}
87120

88121
if ($ErrorCounter -eq 0) {
89-
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Enabled DKIM for all domains in tenant' -sev Info
122+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Enabled DKIM for all domains in tenant' -sev Info
90123
} elseif ($ErrorCounter -gt 0 -and $ErrorCounter -lt ($NewDomains.Count + $SetDomains.Count)) {
91-
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Failed to enable DKIM for some domains in tenant' -sev Error
124+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Failed to enable DKIM for some domains in tenant' -sev Error
92125
} else {
93-
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Failed to enable DKIM for all domains in tenant' -sev Error
126+
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Failed to enable DKIM for all domains in tenant' -sev Error
94127
}
95128
}
96129
}

0 commit comments

Comments
 (0)