|
1 | | -function Remove-CIPPGroupMember( |
2 | | - $Headers, |
3 | | - [string]$GroupType, |
4 | | - [string]$GroupId, |
5 | | - [string]$Member, |
6 | | - [string]$TenantFilter, |
7 | | - [string]$APIName = 'Remove Group Member' |
8 | | -) { |
| 1 | +function Remove-CIPPGroupMember { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Removes members from a Microsoft 365 group. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Removes one or more members from Security Groups, Distribution Groups, or Mail-Enabled Security Groups. |
| 8 | + Uses bulk request operations for Exchange groups to improve performance. |
| 9 | +
|
| 10 | + .PARAMETER Headers |
| 11 | + The headers for the API request, typically containing authentication information. |
| 12 | +
|
| 13 | + .PARAMETER TenantFilter |
| 14 | + The tenant identifier for the target tenant. |
| 15 | +
|
| 16 | + .PARAMETER GroupType |
| 17 | + The type of group. Valid values: 'Distribution list', 'Mail-Enabled Security', or standard security groups. |
| 18 | +
|
| 19 | + .PARAMETER GroupId |
| 20 | + The unique identifier (GUID or name) of the group. |
| 21 | +
|
| 22 | + .PARAMETER Member |
| 23 | + An array of member identifiers (user GUIDs or UPNs) to remove from the group. |
| 24 | +
|
| 25 | + .PARAMETER APIName |
| 26 | + The API operation name for logging purposes. Default: 'Remove Group Member'. |
| 27 | +
|
| 28 | + .EXAMPLE |
| 29 | + Remove-CIPPGroupMember -Headers $Headers -TenantFilter 'contoso.onmicrosoft.com' -GroupType 'Distribution list' -GroupId 'Sales-DL' -Member @('[email protected]', '[email protected]') -APIName 'Remove DL Members' |
| 30 | +
|
| 31 | + .EXAMPLE |
| 32 | + Remove-CIPPGroupMember -Headers $Headers -TenantFilter 'contoso.onmicrosoft.com' -GroupType 'Security' -GroupId '12345-guid' -Member @('user1-guid') |
| 33 | + #> |
| 34 | + [CmdletBinding()] |
| 35 | + param( |
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [string]$TenantFilter, |
| 38 | + |
| 39 | + [Parameter(Mandatory = $true)] |
| 40 | + [string]$GroupType, |
| 41 | + |
| 42 | + [Parameter(Mandatory = $true)] |
| 43 | + [string]$GroupId, |
| 44 | + |
| 45 | + [Parameter(Mandatory = $true)] |
| 46 | + [string[]]$Member, |
| 47 | + |
| 48 | + [Parameter(Mandatory = $false)] |
| 49 | + [string]$APIName = 'Remove Group Member', |
| 50 | + |
| 51 | + $Headers |
| 52 | + ) |
| 53 | + |
9 | 54 | try { |
| 55 | + $Requests = foreach ($m in $Member) { |
| 56 | + if ($m -like '*#EXT#*') { $m = [System.Web.HttpUtility]::UrlEncode($m) } |
| 57 | + @{ |
| 58 | + id = $m |
| 59 | + url = "users/$($m)?`$select=id,userPrincipalName" |
| 60 | + method = 'GET' |
| 61 | + } |
| 62 | + } |
| 63 | + $Users = New-GraphBulkRequest -Requests @($Requests) -tenantid $TenantFilter |
| 64 | + |
10 | 65 | if ($GroupType -eq 'Distribution list' -or $GroupType -eq 'Mail-Enabled Security') { |
11 | | - $Params = @{ Identity = $GroupId; Member = $Member; BypassSecurityGroupManagerCheck = $true } |
12 | | - $null = New-ExoRequest -tenantid $TenantFilter -cmdlet 'Remove-DistributionGroupMember' -cmdParams $Params -UseSystemMailbox $true |
| 66 | + $ExoBulkRequests = [System.Collections.Generic.List[object]]::new() |
| 67 | + $ExoLogs = [System.Collections.Generic.List[object]]::new() |
| 68 | + |
| 69 | + foreach ($User in $Users) { |
| 70 | + $Params = @{ Identity = $GroupId; Member = $User.body.userPrincipalName; BypassSecurityGroupManagerCheck = $true } |
| 71 | + $ExoBulkRequests.Add(@{ |
| 72 | + CmdletInput = @{ |
| 73 | + CmdletName = 'Remove-DistributionGroupMember' |
| 74 | + Parameters = $Params |
| 75 | + } |
| 76 | + }) |
| 77 | + $ExoLogs.Add(@{ |
| 78 | + message = "Removed member $($User.body.userPrincipalName) from $($GroupId) group" |
| 79 | + target = $User.body.userPrincipalName |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + if ($ExoBulkRequests.Count -gt 0) { |
| 84 | + $RawExoRequest = New-ExoBulkRequest -tenantid $TenantFilter -cmdletArray @($ExoBulkRequests) |
| 85 | + $LastError = $RawExoRequest | Select-Object -Last 1 |
| 86 | + |
| 87 | + foreach ($ExoError in $LastError.error) { |
| 88 | + Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $ExoError -Sev 'Error' |
| 89 | + throw $ExoError |
| 90 | + } |
| 91 | + |
| 92 | + foreach ($ExoLog in $ExoLogs) { |
| 93 | + $ExoError = $LastError | Where-Object { $ExoLog.target -in $_.target -and $_.error } |
| 94 | + if (!$LastError -or ($LastError.error -and $LastError.target -notcontains $ExoLog.target)) { |
| 95 | + Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $ExoLog.message -Sev 'Info' |
| 96 | + } |
| 97 | + } |
| 98 | + } |
13 | 99 | } else { |
14 | | - if ($Member -match '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$') { |
15 | | - Write-Information "Member $Member is a GUID, proceeding with removal." |
16 | | - } else { |
17 | | - Write-Information "Member $Member is not a GUID, attempting to resolve to object ID." |
18 | | - if ($Member -like '*#EXT#*') { $Member = [System.Web.HttpUtility]::UrlEncode($Member) } |
19 | | - $UserObject = New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($Member)?`$select=id" -tenantid $TenantFilter |
20 | | - if ($null -eq $UserObject.id) { |
21 | | - throw "Could not resolve user $Member to an object ID." |
| 100 | + $RemovalRequests = foreach ($User in $Users) { |
| 101 | + @{ |
| 102 | + id = $User.body.id |
| 103 | + method = 'DELETE' |
| 104 | + url = "/groups/$($GroupId)/members/$($User.body.id)/`$ref" |
| 105 | + } |
| 106 | + } |
| 107 | + $RemovalResults = New-GraphBulkRequest -tenantid $TenantFilter -Requests @($RemovalRequests) |
| 108 | + foreach ($Result in $RemovalResults) { |
| 109 | + if ($Result.status -ne 204) { |
| 110 | + throw "Failed to remove member $($Result.id): $($Result.body.error.message)" |
22 | 111 | } |
23 | | - $Member = $UserObject.id |
24 | | - Write-Information "Resolved member to object ID: $Member" |
25 | 112 | } |
26 | | - $null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/groups/$($GroupId)/members/$($Member)/`$ref" -tenantid $TenantFilter -type DELETE -body '{}' -Verbose |
27 | 113 | } |
28 | | - $Results = "Successfully removed user $($Member) from $($GroupId)." |
| 114 | + $UserList = ($Users.body.userPrincipalName -join ', ') |
| 115 | + $Results = "Successfully removed user $UserList from $($GroupId)." |
29 | 116 | Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev Info |
30 | 117 | return $Results |
31 | 118 |
|
32 | 119 | } catch { |
33 | 120 | $ErrorMessage = Get-CippException -Exception $_ |
34 | | - $Results = "Failed to remove user $($Member) from $($GroupId): $($ErrorMessage.NormalizedError)" |
| 121 | + $UserList = if ($Users) { ($Users.body.userPrincipalName -join ', ') } else { ($Member -join ', ') } |
| 122 | + $Results = "Failed to remove user $UserList from $($GroupId): $($ErrorMessage.NormalizedError)" |
35 | 123 | Write-LogMessage -headers $Headers -API $APIName -tenant $TenantFilter -message $Results -Sev Error -LogData $ErrorMessage |
36 | 124 | throw $Results |
37 | 125 | } |
|
0 commit comments