Skip to content

Commit 5190635

Browse files
group duplicate fix.
1 parent 6e216e6 commit 5190635

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Modules/CIPPCore/Public/New-CIPPGroup.ps1

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ function New-CIPPGroup {
4141
)
4242

4343
try {
44+
$GroupCacheTable = Get-CIPPTable -tablename 'CacheGroupCreation'
45+
$SafeDisplayName = $GroupObject.displayName -replace '[^a-zA-Z0-9-]', '_'
46+
$CacheRowKey = '{0}_{1}' -f $TenantFilter, $SafeDisplayName
47+
$TenMinutesAgo = (Get-Date).AddMinutes(-10).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ')
48+
$CachedGroup = Get-CIPPAzDataTableEntity @GroupCacheTable -Filter "PartitionKey eq 'GroupCreation' and RowKey eq '$CacheRowKey' and Timestamp ge datetime'$TenMinutesAgo'"
49+
if ($CachedGroup -and $CachedGroup.GroupId) {
50+
Write-LogMessage -API $APIName -tenant $TenantFilter -message "Group '$($GroupObject.displayName)' was recently created (cached id: $($CachedGroup.GroupId)), skipping duplicate creation" -Sev Info
51+
return [PSCustomObject]@{
52+
Success = $true
53+
Message = "Group $($GroupObject.displayName) already exists (from cache)"
54+
GroupId = $CachedGroup.GroupId
55+
GroupType = $CachedGroup.GroupType
56+
Email = $CachedGroup.Email
57+
AlreadyExist = $true
58+
}
59+
}
60+
4461
# Normalize group type for consistent handling (accept camelCase from templates)
4562
$NormalizedGroupType = switch -Wildcard ($GroupObject.groupType.ToLower()) {
4663
'mail-enabled security' { 'Security'; break }
@@ -161,6 +178,16 @@ function New-CIPPGroup {
161178
GroupType = $NormalizedGroupType
162179
Email = if ($NeedsEmail) { $Email } else { $null }
163180
}
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
189+
}
190+
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
164191
if ($GroupObject.subscribeMembers) {
165192
#Waiting for group to become available in Exo.
166193
Start-Sleep -Seconds 10
@@ -240,6 +267,16 @@ function New-CIPPGroup {
240267
GroupType = $NormalizedGroupType
241268
Email = $Email
242269
}
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
278+
}
279+
Add-CIPPAzDataTableEntity @GroupCacheTable -Entity $CacheEntity -Force
243280
}
244281

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

0 commit comments

Comments
 (0)