Skip to content

Commit 01242ff

Browse files
committed
fix(user): route group updates by effective type
Refactor Set-CIPPUser to use a shared helper that decides whether group membership changes should use Exchange cmdlets based on `addedFields.calculatedGroupType`, with fallback to legacy `groupType` values. This fixes inconsistent add/remove behavior caused by the previous inline condition and variable-case mismatch, ensuring distribution lists and mail-enabled security groups are handled consistently.
1 parent fb4dec1 commit 01242ff

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

Modules/CIPPCore/Public/Set-CIPPUser.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ function Set-CIPPUser {
1212
$AddToGroups = $UserObj.AddToGroups
1313
$RemoveFromGroups = $UserObj.RemoveFromGroups
1414

15+
$UseExchangeForGroup = {
16+
param($AddedFields)
17+
$Calculated = $AddedFields.calculatedGroupType
18+
if ($Calculated) {
19+
return $Calculated -in @('distributionList', 'security')
20+
}
21+
return $AddedFields.groupType -in @('Distribution List', 'Mail-Enabled Security', 'distributionList')
22+
}
23+
1524

1625
#Edit the user
1726
try {
@@ -189,13 +198,12 @@ function Set-CIPPUser {
189198
$AddToGroups | ForEach-Object {
190199

191200
$GroupType = $_.addedFields.groupType
192-
$CalculatedGroupType = $_.addedFields.calculatedGroupType ?? $null
193201
$GroupID = $_.value
194202
$GroupName = $_.label
195203
Write-Host "About to add $($UserObj.userPrincipalName) to $GroupName. Group ID is: $GroupID and type is: $GroupType"
196204

197205
try {
198-
if ($GroupType -eq 'distributionList' -or $GroupType -eq 'security' -and ($calculatedGroupType -ne 'generic' )) {
206+
if (& $UseExchangeForGroup $_.addedFields) {
199207
Write-Host 'Adding to group via Add-DistributionGroupMember'
200208
$Params = @{ Identity = $GroupID; Member = $UserObj.id; BypassSecurityGroupManagerCheck = $true }
201209
$null = New-ExoRequest -tenantid $UserObj.tenantFilter -cmdlet 'Add-DistributionGroupMember' -cmdParams $params -UseSystemMailbox $true
@@ -222,13 +230,12 @@ function Set-CIPPUser {
222230
$RemoveFromGroups | ForEach-Object {
223231

224232
$GroupType = $_.addedFields.groupType
225-
$CalculatedGroupType = $_.addedFields.calculatedGroupType ?? $null
226233
$GroupID = $_.value
227234
$GroupName = $_.label
228235
Write-Host "About to remove $($UserObj.userPrincipalName) from $GroupName. Group ID is: $GroupID and type is: $GroupType"
229236

230237
try {
231-
if ($GroupType -eq 'distributionList' -or $GroupType -eq 'security' -and ($calculatedGroupType -ne 'generic' )) {
238+
if (& $UseExchangeForGroup $_.addedFields) {
232239
Write-Host 'Removing From group via Remove-DistributionGroupMember'
233240
$Params = @{ Identity = $GroupID; Member = $UserObj.id; BypassSecurityGroupManagerCheck = $true }
234241
$null = New-ExoRequest -tenantid $UserObj.tenantFilter -cmdlet 'Remove-DistributionGroupMember' -cmdParams $params -UseSystemMailbox $true

0 commit comments

Comments
 (0)