@@ -123,16 +123,19 @@ function New-CIPPCAPolicy {
123123 if (! $locations ) { continue }
124124 foreach ($location in $locations ) {
125125 if (! $location.displayName ) { continue }
126- $CheckExististing = New-GraphGETRequest - uri ' https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' - tenantid $TenantFilter - asApp $true
127- if ($Location.displayName -in $CheckExististing .displayName ) {
126+ $CheckExisting = New-GraphGETRequest - uri ' https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' - tenantid $TenantFilter - asApp $true
127+ if ($Location.displayName -in $CheckExisting .displayName ) {
128128 [pscustomobject ]@ {
129- id = ($CheckExististing | Where-Object - Property displayName -EQ $Location.displayName ).id
130- name = ($CheckExististing | Where-Object - Property displayName -EQ $Location.displayName ).displayName
129+ id = ($CheckExisting | Where-Object - Property displayName -EQ $Location.displayName ).id
130+ name = ($CheckExisting | Where-Object - Property displayName -EQ $Location.displayName ).displayName
131+ templateId = $location.id
131132 }
132133 Write-LogMessage - Headers $User - API $APINAME - message " Matched a CA policy with the existing Named Location: $ ( $location.displayName ) " - Sev ' Info'
133134
134135 } else {
135136 if ($location.countriesAndRegions ) { $location.countriesAndRegions = @ ($location.countriesAndRegions ) }
137+ $location | Select-Object * - ExcludeProperty id
138+ Remove-ODataProperties - Object $location
136139 $Body = ConvertTo-Json - InputObject $Location
137140 $GraphRequest = New-GraphPOSTRequest - uri ' https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' - body $body - Type POST - tenantid $tenantfilter - asApp $true
138141 $retryCount = 0
@@ -151,19 +154,21 @@ function New-CIPPCAPolicy {
151154 }
152155 }
153156 }
157+ Write-Information ' Location Lookup Table:'
158+ Write-Information ($LocationLookupTable | ConvertTo-Json - Depth 10 )
154159
155160 foreach ($location in $JSONobj.conditions.locations.includeLocations ) {
156- Write-Information " Replacing named location - $location "
157- $lookup = $LocationLookupTable | Where-Object - Property name -EQ $location
158- Write-Information " Found $lookup "
161+ $lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location }
159162 if (! $lookup ) { continue }
163+ Write-Information " Replacing named location - $location "
160164 $index = [array ]::IndexOf($JSONobj.conditions.locations.includeLocations , $location )
161165 $JSONobj.conditions.locations.includeLocations [$index ] = $lookup.id
162166 }
163167
164168 foreach ($location in $JSONobj.conditions.locations.excludeLocations ) {
165- $lookup = $LocationLookupTable | Where-Object - Property name -EQ $location
169+ $lookup = $LocationLookupTable | Where-Object { $_ . name -eq $location -or $_ .displayName -eq $location -or $_ .templateId -eq $location }
166170 if (! $lookup ) { continue }
171+ Write-Information " Replacing named location - $location "
167172 $index = [array ]::IndexOf($JSONobj.conditions.locations.excludeLocations , $location )
168173 $JSONobj.conditions.locations.excludeLocations [$index ] = $lookup.id
169174 }
0 commit comments