Skip to content

Commit e1a5622

Browse files
Fix some null safe stuff
1 parent a6ba745 commit e1a5622

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ function New-CIPPCAPolicy {
150150
$JSONObj.conditions.users.$groupType = @(Replace-GroupNameWithId -groupNames $JSONObj.conditions.users.$groupType)
151151
}
152152
}
153+
153154
} catch {
154155
$ErrorMessage = Get-CippException -Exception $_
155156
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to replace displayNames for conditional access rule $($JSONObj.displayName). Error: $($ErrorMessage.NormalizedError)" -sev 'Error' -LogData $ErrorMessage
@@ -158,6 +159,27 @@ function New-CIPPCAPolicy {
158159
}
159160
}
160161
$JsonObj.PSObject.Properties.Remove('LocationInfo')
162+
foreach ($condition in $JSONObj.conditions.users.PSObject.Properties.Name) {
163+
$value = $JSONObj.conditions.users.$condition
164+
if ($null -eq $value) {
165+
$JSONObj.conditions.users.$condition = @()
166+
continue
167+
}
168+
if ($value -is [string]) {
169+
if ([string]::IsNullOrWhiteSpace($value)) {
170+
$JSONObj.conditions.users.$condition = @()
171+
continue
172+
}
173+
}
174+
if ($value -is [array]) {
175+
$nonWhitespaceItems = $value | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
176+
if ($nonWhitespaceItems.Count -eq 0) {
177+
$JSONObj.conditions.users.$condition = @()
178+
continue
179+
}
180+
}
181+
}
182+
161183
$RawJSON = ConvertTo-Json -InputObject $JSONObj -Depth 10 -Compress
162184
Write-Host $RawJSON
163185
try {

0 commit comments

Comments
 (0)