Skip to content

Commit 3386990

Browse files
authored
Merge pull request KelvinTegelaar#1669 from kris6673/fix-json-conversion-error
Fix: error handling in Get-GraphToken
2 parents 3b917c6 + 50d591e commit 3386990

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

Modules/CIPPCore/Public/GraphHelper/Get-GraphToken.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,14 @@ function Get-GraphToken($tenantid, $scope, $AsApp, $AppID, $AppSecret, $refreshT
9595
}
9696
}
9797
$Tenant.LastGraphError = if ( $_.ErrorDetails.Message) {
98-
$msg = $_.ErrorDetails.Message | ConvertFrom-Json
99-
"$($msg.error):$($msg.error_description)"
98+
if (Test-Json $_.ErrorDetails.Message -ErrorAction SilentlyContinue) {
99+
$msg = $_.ErrorDetails.Message | ConvertFrom-Json
100+
"$($msg.error):$($msg.error_description)"
101+
} else {
102+
"$($_.ErrorDetails.Message)"
103+
}
100104
} else {
101-
$_.Exception.message
105+
$_.Exception.Message
102106
}
103107
$Tenant.GraphErrorCount++
104108

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ function Invoke-CIPPStandardAddDMARCToMOERA {
4242
HostName = '_dmarc'
4343
TtlValue = 3600
4444
Type = 'TXT'
45-
Value = $Settings.RecordValue.Value ?? "v=DMARC1; p=reject;"
45+
Value = $Settings.RecordValue.Value ?? 'v=DMARC1; p=reject;'
4646
}
4747

4848
# Get all fallback domains (onmicrosoft.com domains) and check if the DMARC record is set correctly
4949
try {
50-
$Domains = New-GraphGetRequest -scope 'https://admin.microsoft.com/.default' -TenantID $Tenant -Uri 'https://admin.microsoft.com/admin/api/Domains/List' | Where-Object -Property Name -like "*.onmicrosoft.com"
50+
$Domains = New-GraphGetRequest -scope 'https://admin.microsoft.com/.default' -TenantID $Tenant -Uri 'https://admin.microsoft.com/admin/api/Domains/List' | Where-Object -Property Name -Like '*.onmicrosoft.com'
5151

5252
$CurrentInfo = $Domains | ForEach-Object {
5353
# Get current DNS records that matches _dmarc hostname and TXT type
@@ -56,9 +56,9 @@ function Invoke-CIPPStandardAddDMARCToMOERA {
5656
if ($CurrentRecords.count -eq 0) {
5757
#record not found, return a model with Match set to false
5858
[PSCustomObject]@{
59-
DomainName = $_.Name
60-
Match = $false
61-
CurrentRecord = $null
59+
DomainName = $_.Name
60+
Match = $false
61+
CurrentRecord = $null
6262
}
6363
} else {
6464
foreach ($CurrentRecord in $CurrentRecords) {
@@ -73,15 +73,15 @@ function Invoke-CIPPStandardAddDMARCToMOERA {
7373
# Compare the current record with the expected record model
7474
if (!(Compare-Object -ReferenceObject $RecordModel -DifferenceObject $CurrentRecordModel -Property HostName, TtlValue, Type, Value)) {
7575
[PSCustomObject]@{
76-
DomainName = $_.Name
77-
Match = $true
78-
CurrentRecord = $CurrentRecord
76+
DomainName = $_.Name
77+
Match = $true
78+
CurrentRecord = $CurrentRecord
7979
}
8080
} else {
8181
[PSCustomObject]@{
82-
DomainName = $_.Name
83-
Match = $false
84-
CurrentRecord = $CurrentRecord
82+
DomainName = $_.Name
83+
Match = $false
84+
CurrentRecord = $CurrentRecord
8585
}
8686
}
8787
}
@@ -92,32 +92,29 @@ function Invoke-CIPPStandardAddDMARCToMOERA {
9292
} catch {
9393
if ($_.Exception.Message -like '*403*') {
9494
$Message = "AddDMARCToMOERA: Insufficient permissions. Please ensure the tenant GDAP relationship includes the 'Domain Name Administrator' role: $(Get-NormalizedError -message $_.Exception.message)"
95-
}
96-
else {
95+
} else {
9796
$Message = "Failed to get dns records for MOERA domains: $(Get-NormalizedError -message $_.Exception.message)"
9897
}
9998
Write-LogMessage -API 'Standards' -tenant $tenant -message $Message -sev Error
100-
throw $Message
99+
return $Message
101100
}
102101

103-
If ($Settings.remediate -eq $true) {
102+
if ($Settings.remediate -eq $true) {
104103
if ($StateIsCorrect -eq $true) {
105104
Write-LogMessage -API 'Standards' -tenant $tenant -message 'DMARC record is already set for all MOERA (onmicrosoft.com) domains.' -sev Info
106-
}
107-
else {
105+
} else {
108106
# Loop through each domain and set the DMARC record, existing misconfigured records and duplicates will be deleted
109107
foreach ($Domain in ($CurrentInfo | Sort-Object -Property DomainName -Unique)) {
110108
try {
111-
foreach ($Record in ($CurrentInfo | Where-Object -Property DomainName -eq $Domain.DomainName)) {
109+
foreach ($Record in ($CurrentInfo | Where-Object -Property DomainName -EQ $Domain.DomainName)) {
112110
if ($Record.CurrentRecord) {
113-
New-GraphPOSTRequest -tenantid $tenant -scope 'https://admin.microsoft.com/.default' -Uri "https://admin.microsoft.com/admin/api/Domains/Record?domainName=$($Domain.DomainName)" -Body ($Record.CurrentRecord | ConvertTo-Json -Compress) -AddedHeaders @{'x-http-method-override' = 'Delete'}
111+
New-GraphPOSTRequest -tenantid $tenant -scope 'https://admin.microsoft.com/.default' -Uri "https://admin.microsoft.com/admin/api/Domains/Record?domainName=$($Domain.DomainName)" -Body ($Record.CurrentRecord | ConvertTo-Json -Compress) -AddedHeaders @{'x-http-method-override' = 'Delete' }
114112
Write-LogMessage -API 'Standards' -tenant $tenant -message "Deleted incorrect DMARC record for domain $($Domain.DomainName)" -sev Info
115113
}
116-
New-GraphPOSTRequest -tenantid $tenant -scope 'https://admin.microsoft.com/.default' -type "PUT" -Uri "https://admin.microsoft.com/admin/api/Domains/Record?domainName=$($Domain.DomainName)" -Body (@{RecordModel = $RecordModel} | ConvertTo-Json -Compress)
114+
New-GraphPOSTRequest -tenantid $tenant -scope 'https://admin.microsoft.com/.default' -type 'PUT' -Uri "https://admin.microsoft.com/admin/api/Domains/Record?domainName=$($Domain.DomainName)" -Body (@{RecordModel = $RecordModel } | ConvertTo-Json -Compress)
117115
Write-LogMessage -API 'Standards' -tenant $tenant -message "Set DMARC record for domain $($Domain.DomainName)" -sev Info
118116
}
119-
}
120-
catch {
117+
} catch {
121118
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to set DMARC record for domain $($Domain.DomainName): $(Get-NormalizedError -message $_.Exception.message)" -sev Error
122119
}
123120
}
@@ -129,10 +126,10 @@ function Invoke-CIPPStandardAddDMARCToMOERA {
129126
Write-LogMessage -API 'Standards' -tenant $tenant -message 'DMARC record is already set for all MOERA (onmicrosoft.com) domains.' -sev Info
130127
} else {
131128
$UniqueDomains = ($CurrentInfo | Sort-Object -Property DomainName -Unique)
132-
$NotSetDomains = @($UniqueDomains | ForEach-Object {if ($_.Match -eq $false -or ($CurrentInfo | Where-Object -Property DomainName -eq $_.DomainName).Count -eq 1) { $_.DomainName } })
129+
$NotSetDomains = @($UniqueDomains | ForEach-Object { if ($_.Match -eq $false -or ($CurrentInfo | Where-Object -Property DomainName -EQ $_.DomainName).Count -eq 1) { $_.DomainName } })
133130
$Message = "DMARC record is not set for $($NotSetDomains.count) of $($UniqueDomains.count) MOERA (onmicrosoft.com) domains."
134131

135-
Write-StandardsAlert -message $Message -object @{MissingDMARC = ($NotSetDomains -join ', ')} -tenant $tenant -standardName 'AddDMARCToMOERA' -standardId $Settings.standardId
132+
Write-StandardsAlert -message $Message -object @{MissingDMARC = ($NotSetDomains -join ', ') } -tenant $tenant -standardName 'AddDMARCToMOERA' -standardId $Settings.standardId
136133
Write-LogMessage -API 'Standards' -tenant $tenant -message "$Message. Missing for: $($NotSetDomains -join ', ')" -sev Info
137134
}
138135
}

0 commit comments

Comments
 (0)