Skip to content

Commit 94663be

Browse files
committed
Updated New-CIPPCAPolicy to update named locations when overwrite is true
1 parent d6dde87 commit 94663be

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,26 @@ function New-CIPPCAPolicy {
173173
if (!$location.displayName) { continue }
174174
$CheckExisting = New-GraphGETRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -tenantid $TenantFilter -asApp $true
175175
if ($Location.displayName -in $CheckExisting.displayName) {
176+
$ExistingLocation = $CheckExisting | Where-Object -Property displayName -EQ $Location.displayName
177+
if ($Overwrite) {
178+
$LocationUpdate = $location | Select-Object * -ExcludeProperty id
179+
Remove-ODataProperties -Object $LocationUpdate
180+
$Body = ConvertTo-Json -InputObject $LocationUpdate -Depth 10
181+
try {
182+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$($ExistingLocation.id)" -body $body -Type PATCH -tenantid $tenantfilter -asApp $true
183+
Write-LogMessage -Tenant $TenantFilter -Headers $User -API $APINAME -message "Updated existing Named Location: $($location.displayName)" -Sev 'Info'
184+
} catch {
185+
Write-Warning "Failed to update location $($location.displayName): $_"
186+
Write-LogMessage -Tenant $TenantFilter -Headers $User -API $APINAME -message "Failed to update existing Named Location: $($location.displayName). Error: $_" -Sev 'Error'
187+
}
188+
} else {
189+
Write-LogMessage -Tenant $TenantFilter -Headers $User -API $APINAME -message "Matched a CA policy with the existing Named Location: $($location.displayName)" -Sev 'Info'
190+
}
176191
[pscustomobject]@{
177-
id = ($CheckExisting | Where-Object -Property displayName -EQ $Location.displayName).id
178-
name = ($CheckExisting | Where-Object -Property displayName -EQ $Location.displayName).displayName
179-
templateId = $location.id
192+
id = $ExistingLocation.id
193+
name = $ExistingLocation.displayName
194+
templateId = $location.id
180195
}
181-
Write-LogMessage -Tenant $TenantFilter -Headers $User -API $APINAME -message "Matched a CA policy with the existing Named Location: $($location.displayName)" -Sev 'Info'
182-
183196
} else {
184197
if ($location.countriesAndRegions) { $location.countriesAndRegions = @($location.countriesAndRegions) }
185198
$location | Select-Object * -ExcludeProperty id

0 commit comments

Comments
 (0)