Skip to content

Commit

Permalink
Fix some null safe stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Dec 24, 2024
1 parent a6ba745 commit e1a5622
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ function New-CIPPCAPolicy {
$JSONObj.conditions.users.$groupType = @(Replace-GroupNameWithId -groupNames $JSONObj.conditions.users.$groupType)
}
}

} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to replace displayNames for conditional access rule $($JSONObj.displayName). Error: $($ErrorMessage.NormalizedError)" -sev 'Error' -LogData $ErrorMessage
Expand All @@ -158,6 +159,27 @@ function New-CIPPCAPolicy {
}
}
$JsonObj.PSObject.Properties.Remove('LocationInfo')
foreach ($condition in $JSONObj.conditions.users.PSObject.Properties.Name) {
$value = $JSONObj.conditions.users.$condition
if ($null -eq $value) {
$JSONObj.conditions.users.$condition = @()
continue
}
if ($value -is [string]) {
if ([string]::IsNullOrWhiteSpace($value)) {
$JSONObj.conditions.users.$condition = @()
continue
}
}
if ($value -is [array]) {
$nonWhitespaceItems = $value | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
if ($nonWhitespaceItems.Count -eq 0) {
$JSONObj.conditions.users.$condition = @()
continue
}
}
}

$RawJSON = ConvertTo-Json -InputObject $JSONObj -Depth 10 -Compress
Write-Host $RawJSON
try {
Expand Down

0 comments on commit e1a5622

Please sign in to comment.