Skip to content

Commit b1c177e

Browse files
group creation error handling
1 parent cb4c25a commit b1c177e

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

Modules/CIPPCore/Public/New-CIPPCAPolicy.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ function New-CIPPCAPolicy {
6161
$GroupIds.Add($NewGroup.GroupId)
6262
}
6363
} else {
64-
Write-Warning "Group $_ not found in the tenant"
64+
Write-Warning "Group $_ not found in the tenant and CreateGroups is disabled"
65+
throw "Group '$_' not found in tenant $TenantFilter. Enable 'Create groups if they do not exist' or create the group manually before deploying this policy."
6566
}
6667
}
6768
}

Modules/CIPPCore/Public/New-CIPPGroup.ps1

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,20 @@ function New-CIPPGroup {
178178
GroupType = $NormalizedGroupType
179179
Email = if ($NeedsEmail) { $Email } else { $null }
180180
}
181-
$CacheEntity = @{
182-
PartitionKey = 'GroupCreation'
183-
RowKey = $CacheRowKey
184-
GroupId = [string]$GraphRequest.id
185-
DisplayName = [string]$GroupObject.displayName
186-
GroupType = [string]$NormalizedGroupType
187-
Email = [string]$(if ($NeedsEmail) { $Email } else { '' })
188-
Tenant = [string]$TenantFilter
181+
try {
182+
$CacheEntity = @{
183+
PartitionKey = 'GroupCreation'
184+
RowKey = $CacheRowKey
185+
GroupId = [string]$GraphRequest.id
186+
DisplayName = [string]$GroupObject.displayName
187+
GroupType = [string]$NormalizedGroupType
188+
Email = [string]$(if ($NeedsEmail) { $Email } else { '' })
189+
Tenant = [string]$TenantFilter
190+
}
191+
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
192+
} catch {
193+
Write-Warning "Failed to write group creation cache for $($GroupObject.displayName): $($_.Exception.Message)"
189194
}
190-
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
191195
if ($GroupObject.subscribeMembers) {
192196
#Waiting for group to become available in Exo.
193197
Start-Sleep -Seconds 10
@@ -267,16 +271,20 @@ function New-CIPPGroup {
267271
GroupType = $NormalizedGroupType
268272
Email = $Email
269273
}
270-
$CacheEntity = @{
271-
PartitionKey = 'GroupCreation'
272-
RowKey = $CacheRowKey
273-
GroupId = [string]$GraphRequest.Identity
274-
DisplayName = [string]$GroupObject.displayName
275-
GroupType = [string]$NormalizedGroupType
276-
Email = [string]$Email
277-
Tenant = [string]$TenantFilter
274+
try {
275+
$CacheEntity = @{
276+
PartitionKey = 'GroupCreation'
277+
RowKey = $CacheRowKey
278+
GroupId = [string]$GraphRequest.Identity
279+
DisplayName = [string]$GroupObject.displayName
280+
GroupType = [string]$NormalizedGroupType
281+
Email = [string]$Email
282+
Tenant = [string]$TenantFilter
283+
}
284+
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
285+
} catch {
286+
Write-Warning "Failed to write group creation cache for $($GroupObject.displayName): $($_.Exception.Message)"
278287
}
279-
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
280288
}
281289

282290
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Created group $($GroupObject.displayName) with id $($Result.GroupId)" -Sev Info

0 commit comments

Comments
 (0)