Skip to content

Commit 6c897f5

Browse files
committed
fixes group addition in user page and vacation mode
1 parent 2511c6e commit 6c897f5

2 files changed

Lines changed: 24 additions & 48 deletions

File tree

Modules/CIPPCore/Public/Add-CIPPGroupMember.ps1

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,54 +45,37 @@ function Add-CIPPGroupMember {
4545
}
4646
$Users = New-GraphBulkRequest -Requests @($Requests) -tenantid $TenantFilter
4747

48-
$SuccessfulUsers = [System.Collections.Generic.List[string]]::new()
49-
$FailedUsers = [System.Collections.Generic.List[string]]::new()
50-
51-
# Accept both human-readable labels (from Invoke-EditGroup / older callers) and
52-
# camelCase calculatedGroupType values (from the user template / add-edit-user form)
53-
$ExoGroupTypes = @('Distribution list', 'Distribution List', 'Mail-Enabled Security', 'distributionList', 'security')
54-
55-
if ($GroupType -in $ExoGroupTypes) {
48+
if ($GroupType -eq 'Distribution list' -or $GroupType -eq 'Mail-Enabled Security') {
5649
$ExoBulkRequests = [System.Collections.Generic.List[object]]::new()
57-
$GuidToUpn = @{}
50+
$ExoLogs = [System.Collections.Generic.List[object]]::new()
5851

5952
foreach ($User in $Users) {
60-
$UserUpn = $User.body.userPrincipalName
61-
if (-not $UserUpn) { continue }
62-
$OpGuid = [guid]::NewGuid().ToString()
63-
$GuidToUpn[$OpGuid] = $UserUpn
64-
$Params = @{ Identity = $GroupId; Member = $UserUpn; BypassSecurityGroupManagerCheck = $true }
53+
$Params = @{ Identity = $GroupId; Member = $User.body.userPrincipalName; BypassSecurityGroupManagerCheck = $true }
6554
$ExoBulkRequests.Add(@{
66-
OperationGuid = $OpGuid
67-
CmdletInput = @{
55+
CmdletInput = @{
6856
CmdletName = 'Add-DistributionGroupMember'
6957
Parameters = $Params
7058
}
7159
})
60+
$ExoLogs.Add(@{
61+
message = "Added member $($User.body.userPrincipalName) to $($GroupId) group"
62+
target = $User.body.userPrincipalName
63+
})
7264
}
7365

7466
if ($ExoBulkRequests.Count -gt 0) {
75-
$RawExoRequest = @(New-ExoBulkRequest -tenantid $TenantFilter -cmdletArray @($ExoBulkRequests))
67+
$RawExoRequest = New-ExoBulkRequest -tenantid $TenantFilter -cmdletArray @($ExoBulkRequests)
68+
$LastError = $RawExoRequest | Select-Object -Last 1
7669

77-
# Index responses by OperationGuid so each user is correlated by position, not by error.target
78-
$ResponseByGuid = @{}
79-
foreach ($Response in $RawExoRequest) {
80-
if ($Response.OperationGuid) {
81-
$ResponseByGuid[$Response.OperationGuid] = $Response
82-
}
70+
foreach ($ExoError in $LastError.error) {
71+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $ExoError -Sev 'Error'
72+
throw $ExoError
8373
}
8474

85-
foreach ($OpGuid in $GuidToUpn.Keys) {
86-
$UserUpn = $GuidToUpn[$OpGuid]
87-
$Response = $ResponseByGuid[$OpGuid]
88-
89-
if ($Response -and $Response.error) {
90-
$ErrorText = if ($Response.error -is [string]) { $Response.error } else { ($Response.error | Out-String).Trim() }
91-
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to add member $($UserUpn) to $($GroupId): $ErrorText" -Sev 'Error'
92-
$FailedUsers.Add($UserUpn)
93-
} else {
94-
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Added member $($UserUpn) to $($GroupId) group" -Sev 'Info'
95-
$SuccessfulUsers.Add($UserUpn)
75+
foreach ($ExoLog in $ExoLogs) {
76+
$ExoError = $LastError | Where-Object { $ExoLog.target -in $_.target -and $_.error }
77+
if (!$LastError -or ($LastError.error -and $LastError.target -notcontains $ExoLog.target)) {
78+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $ExoLog.message -Sev 'Info'
9679
}
9780
}
9881
}
@@ -108,26 +91,19 @@ function Add-CIPPGroupMember {
10891
}
10992
}
11093
$AddResults = New-GraphBulkRequest -tenantid $TenantFilter -Requests @($AddRequests)
94+
$SuccessfulUsers = [system.collections.generic.list[string]]::new()
11195
foreach ($Result in $AddResults) {
112-
$UserPrincipalName = $Users | Where-Object { $_.body.id -eq $Result.id } | Select-Object -ExpandProperty body | Select-Object -ExpandProperty userPrincipalName
11396
if ($Result.status -lt 200 -or $Result.status -gt 299) {
114-
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to add member $($UserPrincipalName): $($Result.body.error.message)" -Sev 'Error'
115-
$FailedUsers.Add($UserPrincipalName)
97+
$FailedUsername = $Users | Where-Object { $_.body.id -eq $Result.id } | Select-Object -ExpandProperty body | Select-Object -ExpandProperty userPrincipalName
98+
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message "Failed to add member $($FailedUsername): $($Result.body.error.message)" -Sev 'Error'
11699
} else {
100+
$UserPrincipalName = $Users | Where-Object { $_.body.id -eq $Result.id } | Select-Object -ExpandProperty body | Select-Object -ExpandProperty userPrincipalName
117101
$SuccessfulUsers.Add($UserPrincipalName)
118102
}
119103
}
120104
}
121-
122-
if ($SuccessfulUsers.Count -eq 0 -and $FailedUsers.Count -gt 0) {
123-
$Results = "Failed to add user $($FailedUsers -join ', ') to $($GroupId)."
124-
throw $Results
125-
}
126-
127-
$Results = "Successfully added user $($SuccessfulUsers -join ', ') to $($GroupId)."
128-
if ($FailedUsers.Count -gt 0) {
129-
$Results = "$Results Failed to add: $($FailedUsers -join ', ')."
130-
}
105+
$UserList = ($SuccessfulUsers -join ', ')
106+
$Results = "Successfully added user $UserList to $($GroupId)."
131107
Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev 'Info'
132108
return $Results
133109
} catch {

Modules/CIPPCore/Public/New-CIPPUserTask.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function New-CIPPUserTask {
7070

7171
# Add to groups
7272
if ($UserObj.AddToGroups) {
73-
$ExoGroupTypes = @('Distribution list', 'Distribution List', 'Mail-Enabled Security', 'distributionList', 'security')
73+
$ExoGroupTypes = @('Distribution list', 'Mail-Enabled Security')
7474
$UserObj.AddToGroups | ForEach-Object {
7575
$Group = $_
7676
$GroupType = $Group.addedFields.groupType

0 commit comments

Comments
 (0)