Skip to content

Commit 9a61b67

Browse files
committed
fix issue with state changes on CA deployment
ticket 27684272542
1 parent d2c8357 commit 9a61b67

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ function New-CIPPCAPolicy {
8888

8989
$displayname = ($RawJSON | ConvertFrom-Json).Displayname
9090

91-
$JSONObj = $RawJSON | ConvertFrom-Json | Select-Object * -ExcludeProperty ID, GUID, *time*
92-
Remove-EmptyArrays $JSONObj
91+
$JSONobj = $RawJSON | ConvertFrom-Json | Select-Object * -ExcludeProperty ID, GUID, *time*
92+
Remove-EmptyArrays $JSONobj
9393
#Remove context as it does not belong in the payload.
9494
try {
95-
$JsonObj.grantControls.PSObject.Properties.Remove('[email protected]')
96-
$JSONObj.templateId ? $JSONObj.PSObject.Properties.Remove('templateId') : $null
97-
if ($JSONObj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.Members) {
98-
$JsonObj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.PSObject.Properties.Remove('@odata.context')
95+
$JSONobj.grantControls.PSObject.Properties.Remove('[email protected]')
96+
$JSONobj.templateId ? $JSONobj.PSObject.Properties.Remove('templateId') : $null
97+
if ($JSONobj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.Members) {
98+
$JSONobj.conditions.users.excludeGuestsOrExternalUsers.externalTenants.PSObject.Properties.Remove('@odata.context')
9999
}
100100
if ($State -and $State -ne 'donotchange') {
101-
$Jsonobj.state = $State
101+
$JSONobj.state = $State
102102
}
103103
} catch {
104104
# no issues here.
@@ -108,18 +108,18 @@ function New-CIPPCAPolicy {
108108
if ($JSONobj.GrantControls.authenticationStrength.policyType -eq 'custom' -or $JSONobj.GrantControls.authenticationStrength.policyType -eq 'BuiltIn') {
109109
$ExistingStrength = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/authenticationStrength/policies/' -tenantid $TenantFilter -asApp $true | Where-Object -Property displayName -EQ $JSONobj.GrantControls.authenticationStrength.displayName
110110
if ($ExistingStrength) {
111-
$JSONObj.GrantControls.authenticationStrength = @{ id = $ExistingStrength.id }
111+
$JSONobj.GrantControls.authenticationStrength = @{ id = $ExistingStrength.id }
112112

113113
} else {
114-
$Body = ConvertTo-Json -InputObject $JSONObj.GrantControls.authenticationStrength
114+
$Body = ConvertTo-Json -InputObject $JSONobj.GrantControls.authenticationStrength
115115
$GraphRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/authenticationStrength/policies' -body $body -Type POST -tenantid $tenantfilter -asApp $true
116-
$JSONObj.GrantControls.authenticationStrength = @{ id = $ExistingStrength.id }
117-
Write-LogMessage -Headers $User -API $APINAME -message "Created new Authentication Strength Policy: $($JSONObj.GrantControls.authenticationStrength.displayName)" -Sev 'Info'
116+
$JSONobj.GrantControls.authenticationStrength = @{ id = $ExistingStrength.id }
117+
Write-LogMessage -Headers $User -API $APINAME -message "Created new Authentication Strength Policy: $($JSONobj.GrantControls.authenticationStrength.displayName)" -Sev 'Info'
118118
}
119119
}
120120

121-
#for each of the locations, check if they exist, if not create them. These are in $jsonobj.LocationInfo
122-
$LocationLookupTable = foreach ($locations in $jsonobj.LocationInfo) {
121+
#for each of the locations, check if they exist, if not create them. These are in $JSONobj.LocationInfo
122+
$LocationLookupTable = foreach ($locations in $JSONobj.LocationInfo) {
123123
if (!$locations) { continue }
124124
foreach ($location in $locations) {
125125
if (!$location.displayName) { continue }
@@ -152,20 +152,20 @@ function New-CIPPCAPolicy {
152152
}
153153
}
154154

155-
foreach ($location in $JSONObj.conditions.locations.includeLocations) {
155+
foreach ($location in $JSONobj.conditions.locations.includeLocations) {
156156
Write-Information "Replacing named location - $location"
157157
$lookup = $LocationLookupTable | Where-Object -Property name -EQ $location
158158
Write-Information "Found $lookup"
159159
if (!$lookup) { continue }
160-
$index = [array]::IndexOf($JSONObj.conditions.locations.includeLocations, $location)
161-
$JSONObj.conditions.locations.includeLocations[$index] = $lookup.id
160+
$index = [array]::IndexOf($JSONobj.conditions.locations.includeLocations, $location)
161+
$JSONobj.conditions.locations.includeLocations[$index] = $lookup.id
162162
}
163163

164-
foreach ($location in $JSONObj.conditions.locations.excludeLocations) {
164+
foreach ($location in $JSONobj.conditions.locations.excludeLocations) {
165165
$lookup = $LocationLookupTable | Where-Object -Property name -EQ $location
166166
if (!$lookup) { continue }
167-
$index = [array]::IndexOf($JSONObj.conditions.locations.excludeLocations, $location)
168-
$JSONObj.conditions.locations.excludeLocations[$index] = $lookup.id
167+
$index = [array]::IndexOf($JSONobj.conditions.locations.excludeLocations, $location)
168+
$JSONobj.conditions.locations.excludeLocations[$index] = $lookup.id
169169
}
170170
switch ($ReplacePattern) {
171171
'none' {
@@ -174,10 +174,10 @@ function New-CIPPCAPolicy {
174174
}
175175
'AllUsers' {
176176
Write-Information 'Replacement pattern for inclusions and exclusions is All users. This policy will now apply to everyone.'
177-
if ($JSONObj.conditions.users.includeUsers -ne 'All') { $JSONObj.conditions.users.includeUsers = @('All') }
178-
if ($JSONObj.conditions.users.excludeUsers) { $JSONObj.conditions.users.excludeUsers = @() }
179-
if ($JSONObj.conditions.users.includeGroups) { $JSONObj.conditions.users.includeGroups = @() }
180-
if ($JSONObj.conditions.users.excludeGroups) { $JSONObj.conditions.users.excludeGroups = @() }
177+
if ($JSONobj.conditions.users.includeUsers -ne 'All') { $JSONobj.conditions.users.includeUsers = @('All') }
178+
if ($JSONobj.conditions.users.excludeUsers) { $JSONobj.conditions.users.excludeUsers = @() }
179+
if ($JSONobj.conditions.users.includeGroups) { $JSONobj.conditions.users.includeGroups = @() }
180+
if ($JSONobj.conditions.users.excludeGroups) { $JSONobj.conditions.users.excludeGroups = @() }
181181
}
182182
'displayName' {
183183
try {
@@ -186,41 +186,41 @@ function New-CIPPCAPolicy {
186186
$groups = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/groups?$select=id,displayName' -tenantid $TenantFilter -asApp $true
187187

188188
foreach ($userType in 'includeUsers', 'excludeUsers') {
189-
if ($JSONObj.conditions.users.PSObject.Properties.Name -contains $userType -and $JSONObj.conditions.users.$userType -notin 'All', 'None', 'GuestOrExternalUsers') {
190-
$JSONObj.conditions.users.$userType = @(Replace-UserNameWithId -userNames $JSONObj.conditions.users.$userType)
189+
if ($JSONobj.conditions.users.PSObject.Properties.Name -contains $userType -and $JSONobj.conditions.users.$userType -notin 'All', 'None', 'GuestOrExternalUsers') {
190+
$JSONobj.conditions.users.$userType = @(Replace-UserNameWithId -userNames $JSONobj.conditions.users.$userType)
191191
}
192192
}
193193

194194
# Check the included and excluded groups
195195
foreach ($groupType in 'includeGroups', 'excludeGroups') {
196-
if ($JSONObj.conditions.users.PSObject.Properties.Name -contains $groupType) {
197-
$JSONObj.conditions.users.$groupType = @(Replace-GroupNameWithId -groupNames $JSONObj.conditions.users.$groupType)
196+
if ($JSONobj.conditions.users.PSObject.Properties.Name -contains $groupType) {
197+
$JSONobj.conditions.users.$groupType = @(Replace-GroupNameWithId -groupNames $JSONobj.conditions.users.$groupType)
198198
}
199199
}
200200
} catch {
201201
$ErrorMessage = Get-CippException -Exception $_
202-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to replace displayNames for conditional access rule $($JSONObj.displayName). Error: $($ErrorMessage.NormalizedError)" -sev 'Error' -LogData $ErrorMessage
203-
throw "Failed to replace displayNames for conditional access rule $($JSONObj.displayName): $($ErrorMessage.NormalizedError)"
202+
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to replace displayNames for conditional access rule $($JSONobj.displayName). Error: $($ErrorMessage.NormalizedError)" -sev 'Error' -LogData $ErrorMessage
203+
throw "Failed to replace displayNames for conditional access rule $($JSONobj.displayName): $($ErrorMessage.NormalizedError)"
204204
}
205205
}
206206
}
207-
$JsonObj.PSObject.Properties.Remove('LocationInfo')
208-
foreach ($condition in $JSONObj.conditions.users.PSObject.Properties.Name) {
209-
$value = $JSONObj.conditions.users.$condition
207+
$JSONobj.PSObject.Properties.Remove('LocationInfo')
208+
foreach ($condition in $JSONobj.conditions.users.PSObject.Properties.Name) {
209+
$value = $JSONobj.conditions.users.$condition
210210
if ($null -eq $value) {
211-
$JSONObj.conditions.users.$condition = @()
211+
$JSONobj.conditions.users.$condition = @()
212212
continue
213213
}
214214
if ($value -is [string]) {
215215
if ([string]::IsNullOrWhiteSpace($value)) {
216-
$JSONObj.conditions.users.$condition = @()
216+
$JSONobj.conditions.users.$condition = @()
217217
continue
218218
}
219219
}
220220
if ($value -is [array]) {
221221
$nonWhitespaceItems = $value | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }
222222
if ($nonWhitespaceItems.Count -eq 0) {
223-
$JSONObj.conditions.users.$condition = @()
223+
$JSONobj.conditions.users.$condition = @()
224224
continue
225225
}
226226
}
@@ -237,7 +237,7 @@ function New-CIPPCAPolicy {
237237
Write-Information "Failed to disable security defaults for tenant $($TenantFilter): $($ErrorMessage.NormalizedError)"
238238
}
239239
}
240-
$RawJSON = ConvertTo-Json -InputObject $JSONObj -Depth 10 -Compress
240+
$RawJSON = ConvertTo-Json -InputObject $JSONobj -Depth 10 -Compress
241241
Write-Information $RawJSON
242242
try {
243243
Write-Information 'Checking for existing policies'
@@ -247,27 +247,31 @@ function New-CIPPCAPolicy {
247247
throw "Conditional Access Policy with Display Name $($Displayname) Already exists"
248248
return $false
249249
} else {
250+
if ($State -eq 'donotchange') {
251+
$JSONobj.state = $CheckExististing.state
252+
$RawJSON = ConvertTo-Json -InputObject $JSONobj -Depth 10 -Compress
253+
}
250254
Write-Information "overwriting $($CheckExististing.id)"
251255
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($CheckExististing.id)" -tenantid $tenantfilter -type PATCH -body $RawJSON -asApp $true
252-
Write-LogMessage -Headers $User -API 'Create CA Policy' -tenant $($Tenant) -message "Updated Conditional Access Policy $($JSONObj.Displayname) to the template standard." -Sev 'Info'
256+
Write-LogMessage -Headers $User -API 'Create CA Policy' -tenant $($Tenant) -message "Updated Conditional Access Policy $($JSONobj.Displayname) to the template standard." -Sev 'Info'
253257
return "Updated policy $displayname for $tenantfilter"
254258
}
255259
} else {
256260
Write-Information 'Creating new policy'
257-
if ($JSONobj.GrantControls.authenticationStrength.policyType -or $JSONObj.$jsonobj.LocationInfo) {
261+
if ($JSOObj.GrantControls.authenticationStrength.policyType -or $JSONobj.$JSONobj.LocationInfo) {
258262
Start-Sleep 3
259263
}
260264
$null = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/policies' -tenantid $tenantfilter -type POST -body $RawJSON -asApp $true
261-
Write-LogMessage -Headers $User -API 'Create CA Policy' -tenant $($Tenant) -message "Added Conditional Access Policy $($JSONObj.Displayname)" -Sev 'Info'
265+
Write-LogMessage -Headers $User -API 'Create CA Policy' -tenant $($Tenant) -message "Added Conditional Access Policy $($JSONobj.Displayname)" -Sev 'Info'
262266
return "Created policy $displayname for $tenantfilter"
263267
}
264268
} catch {
265269
$ErrorMessage = Get-CippException -Exception $_
266-
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to create or update conditional access rule $($JSONObj.displayName): $($ErrorMessage.NormalizedError) " -sev 'Error' -LogData $ErrorMessage
270+
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to create or update conditional access rule $($JSONobj.displayName): $($ErrorMessage.NormalizedError) " -sev 'Error' -LogData $ErrorMessage
267271

268-
Write-Warning "Failed to create or update conditional access rule $($JSONObj.displayName): $($ErrorMessage.NormalizedError)"
272+
Write-Warning "Failed to create or update conditional access rule $($JSONobj.displayName): $($ErrorMessage.NormalizedError)"
269273
Write-Information $_.InvocationInfo.PositionMessage
270-
Write-Information ($JSONObj | ConvertTo-Json -Depth 10)
271-
throw "Failed to create or update conditional access rule $($JSONObj.displayName): $($ErrorMessage.NormalizedError)"
274+
Write-Information ($JSONobj | ConvertTo-Json -Depth 10)
275+
throw "Failed to create or update conditional access rule $($JSONobj.displayName): $($ErrorMessage.NormalizedError)"
272276
}
273277
}

0 commit comments

Comments
 (0)