Skip to content

Commit 941e521

Browse files
Merge branch 'dev' of https://github.com/KelvinTegelaar/CIPP-API into dev
2 parents d42f25d + 52d538b commit 941e521

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ function New-CIPPCAPolicy {
2525
Write-LogMessage -Headers $Headers -API $APIName -message "Already GUID, no need to replace: $_" -Sev 'Debug'
2626
$GroupIds.Add($_) # it's a GUID, so we keep it
2727
} else {
28-
$groupId = ($groups | Where-Object -Property displayName -EQ $_).id # it's a display name, so we get the group ID
28+
$matchedGroups = @($groups | Where-Object -Property displayName -EQ $_)
29+
$groupId = $matchedGroups.id # it's a display name, so we get the group ID
2930
if ($groupId) {
31+
if ($matchedGroups.Count -gt 1) {
32+
Write-Warning "Multiple groups found with display name '$_'. Using the first match: $($matchedGroups[0].id). IDs found: $($groupId -join ', ')"
33+
$null = Write-LogMessage -Headers $Headers -API $APIName -message "Multiple groups found with display name '$_'. Using first match: $($matchedGroups[0].id)" -Sev 'Warn'
34+
$groupId = @($matchedGroups[0].id)
35+
}
3036
foreach ($gid in $groupId) {
3137
Write-Warning "Replaced group name $_ with ID $gid"
3238
$null = Write-LogMessage -Headers $Headers -API $APIName -message "Replaced group name $_ with ID $gid" -Sev 'Debug'
@@ -218,7 +224,12 @@ function New-CIPPCAPolicy {
218224
if (!$location.displayName) { continue }
219225
# Use cached named locations instead of fetching each time
220226
if ($Location.displayName -in $AllNamedLocations.displayName) {
221-
$ExistingLocation = $AllNamedLocations | Where-Object -Property displayName -EQ $Location.displayName
227+
$ExistingLocation = @($AllNamedLocations | Where-Object -Property displayName -EQ $Location.displayName)
228+
if ($ExistingLocation.Count -gt 1) {
229+
Write-Warning "Multiple named locations found with display name '$($Location.displayName)'. Using the first match: $($ExistingLocation[0].id). IDs found: $($ExistingLocation.id -join ', ')"
230+
Write-LogMessage -Tenant $TenantFilter -Headers $Headers -API $APIName -message "Multiple named locations found with display name '$($Location.displayName)'. Using first match: $($ExistingLocation[0].id)" -Sev 'Warn'
231+
}
232+
$ExistingLocation = $ExistingLocation[0]
222233
if ($Overwrite) {
223234
$LocationUpdate = $location | Select-Object * -ExcludeProperty id
224235
Remove-ODataProperties -Object $LocationUpdate
@@ -275,8 +286,9 @@ function New-CIPPCAPolicy {
275286
throw "Failed to create named location $($location.displayName): $($ErrorMessage.NormalizedError)"
276287
}
277288
[pscustomobject]@{
278-
id = $GraphRequest.id
279-
name = $GraphRequest.displayName
289+
id = $GraphRequest.id
290+
name = $GraphRequest.displayName
291+
templateId = $location.id
280292
}
281293
}
282294
}
@@ -287,7 +299,7 @@ function New-CIPPCAPolicy {
287299
if ($LocationLookupTable -and $JSONobj.conditions.locations) {
288300
foreach ($location in $JSONobj.conditions.locations.includeLocations) {
289301
if ($null -eq $location) { continue }
290-
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location }
302+
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location } | Select-Object -First 1
291303
if (!$lookup) { continue }
292304
Write-Information "Replacing named location - $location"
293305
$index = [array]::IndexOf($JSONobj.conditions.locations.includeLocations, $location)
@@ -298,7 +310,7 @@ function New-CIPPCAPolicy {
298310

299311
foreach ($location in $JSONobj.conditions.locations.excludeLocations) {
300312
if ($null -eq $location) { continue }
301-
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location }
313+
$lookup = $LocationLookupTable | Where-Object { $_.name -eq $location -or $_.displayName -eq $location -or $_.templateId -eq $location } | Select-Object -First 1
302314
if (!$lookup) { continue }
303315
Write-Information "Replacing named location - $location"
304316
$index = [array]::IndexOf($JSONobj.conditions.locations.excludeLocations, $location)
@@ -432,7 +444,7 @@ function New-CIPPCAPolicy {
432444
# Preserve any exclusion groups named "Vacation Exclusion - <PolicyDisplayName>" from existing policy
433445
try {
434446
$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 |
435-
Where-Object { $CheckExisting.conditions.users.excludeGroups -contains $_.id }
447+
Where-Object { $CheckExisting.conditions.users.excludeGroups -contains $_.id }
436448
if ($ExistingVacationGroup) {
437449
if (-not ($JSONobj.conditions.users.PSObject.Properties.Name -contains 'excludeGroups')) {
438450
$JSONobj.conditions.users | Add-Member -NotePropertyName 'excludeGroups' -NotePropertyValue @() -Force
@@ -471,6 +483,7 @@ function New-CIPPCAPolicy {
471483
}
472484
} catch {
473485
$ErrorMessage = Get-CippException -Exception $_
486+
$ErrorMessage | Add-Member -NotePropertyName 'PolicyJSON' -NotePropertyValue $RawJSON -Force
474487
$Result = "Failed to create or update conditional access rule $($JSONobj.displayName): $($ErrorMessage.NormalizedError)"
475488

476489
# Full error details for debugging

0 commit comments

Comments
 (0)