Skip to content

Commit 9aa02ea

Browse files
committed
Guard against null grantControls before removal
Add a null-check for $JSONobj.grantControls before removing the 'authenticationStrength@odata.context' property to prevent errors when grantControls is absent. Also adjust indentation/formatting of a Where-Object call for readability; other payload-cleanup logic is unchanged.
1 parent 44d6eb8 commit 9aa02ea

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ function New-CIPPCAPolicy {
9292
Remove-EmptyArrays -Object $JSONobj
9393
#Remove context as it does not belong in the payload.
9494
try {
95-
$JSONobj.grantControls.PSObject.Properties.Remove('authenticationStrength@odata.context')
95+
if ($JSONobj.grantControls) {
96+
$JSONobj.grantControls.PSObject.Properties.Remove('authenticationStrength@odata.context')
97+
}
9698
$JSONobj.templateId ? $JSONobj.PSObject.Properties.Remove('templateId') : $null
9799
if ($JSONobj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.Members) {
98100
$JSONobj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.PSObject.Properties.Remove('@odata.context')
@@ -426,7 +428,7 @@ function New-CIPPCAPolicy {
426428
# Preserve any exclusion groups named "Vacation Exclusion - <PolicyDisplayName>" from existing policy
427429
try {
428430
$ExistingVacationGroup = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/groups?`$filter=startsWith(displayName,'Vacation Exclusion')&`$select=id,displayName&`$top=999&`$count=true" -ComplexFilter -tenantid $TenantFilter -asApp $true |
429-
Where-Object { $CheckExisting.conditions.users.excludeGroups -contains $_.id }
431+
Where-Object { $CheckExisting.conditions.users.excludeGroups -contains $_.id }
430432
if ($ExistingVacationGroup) {
431433
if (-not ($JSONobj.conditions.users.PSObject.Properties.Name -contains 'excludeGroups')) {
432434
$JSONobj.conditions.users | Add-Member -NotePropertyName 'excludeGroups' -NotePropertyValue @() -Force

0 commit comments

Comments
 (0)