From e1a5622a079d295f518596a97d9ff6c087a1e072 Mon Sep 17 00:00:00 2001 From: KelvinTegelaar <49186168+KelvinTegelaar@users.noreply.github.com> Date: Tue, 24 Dec 2024 21:12:02 +0100 Subject: [PATCH] Fix some null safe stuff --- Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1 | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1 b/Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1 index e75847a094cf..3d5f1981d32a 100644 --- a/Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1 +++ b/Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1 @@ -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 @@ -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 {