@@ -16,10 +16,11 @@ function Invoke-EditGroup {
1616 $UserObj = $Request.Body
1717 $GroupType = $UserObj.groupId.addedFields.groupType ? $UserObj.groupId.addedFields.groupType : $UserObj.groupType
1818 $GroupName = $UserObj.groupName ? $UserObj.groupName : $UserObj.groupId.addedFields.groupName
19- $OrgGroup = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/groups/$ ( $UserObj.groupId ) " - tenantid $UserObj.tenantFilter
19+ $GroupId = $UserObj.groupId.value ?? $UserObj.groupId
20+ $OrgGroup = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/groups/$ ( $GroupId ) " - tenantid $UserObj.tenantFilter
2021
2122 $AddMembers = $UserObj.AddMember
22- $UserObj .groupId = $UserObj .groupId.value ?? $UserObj .groupId
23+
2324
2425 $TenantId = $UserObj.tenantId ?? $UserObj.tenantFilter
2526
@@ -29,36 +30,38 @@ function Invoke-EditGroup {
2930 $ExoBulkRequests = [System.Collections.Generic.List [object ]]::new()
3031 $ExoLogs = [System.Collections.Generic.List [object ]]::new()
3132
32- # Edit properties:
33- if ($GroupType -eq ' Distribution List' -or $GroupType -eq ' Mail-Enabled Security' ) {
34- $Params = @ { Identity = $UserObj.groupId ; DisplayName = $UserObj.displayName ; Description = $UserObj.description ; name = $UserObj.mailNickname }
35- $ExoBulkRequests.Add (@ {
36- CmdletInput = @ {
37- CmdletName = ' Set-DistributionGroup'
38- Parameters = $Params
39- }
40- })
41- $ExoLogs.Add (@ {
42- message = " Success - Edited group properties for $ ( $GroupName ) group. It might take some time to reflect the changes."
43- target = $UserObj.groupId
44- })
45- } else {
46- $PatchObj = @ {
47- displayName = $UserObj.displayName
48- description = $UserObj.description
49- mailNickname = $UserObj.mailNickname
50- mailEnabled = $OrgGroup.mailEnabled
51- securityEnabled = $OrgGroup.securityEnabled
52- }
53- Write-Host " body: $ ( $PatchObj | ConvertTo-Json - Depth 10 - Compress) " - ForegroundColor Yellow
54- if ($UserObj.membershipRules ) { $PatchObj | Add-Member - MemberType NoteProperty - Name ' membershipRule' - Value $UserObj.membershipRules - Force }
55- try {
56- $patch = New-GraphPOSTRequest - type PATCH - uri " https://graph.microsoft.com/beta/groups/$ ( $UserObj.groupId ) " - tenantid $UserObj.tenantFilter - body ($PatchObj | ConvertTo-Json - Depth 10 - Compress)
57- $Results.Add (" Success - Edited group properties for $ ( $GroupName ) group. It might take some time to reflect the changes." )
58- Write-LogMessage - headers $Headers - API $APIName - tenant $UserObj.tenantFilter - message " Edited group properties for $ ( $GroupName ) group" - Sev ' Info'
59- } catch {
60- $Results.Add (" Error - Failed to edit group properties: $ ( $_.Exception.Message ) " )
61- Write-LogMessage - headers $Headers - API $APIName - tenant $UserObj.tenantFilter - message " Failed to patch group: $ ( $_.Exception.Message ) " - Sev ' Error'
33+ if ($UserObj.displayName -or $UserObj.description -or $UserObj.mailNickname -or $UserObj.membershipRules ) {
34+ # Edit properties:
35+ if ($GroupType -eq ' Distribution List' -or $GroupType -eq ' Mail-Enabled Security' ) {
36+ $Params = @ { Identity = $GroupId ; DisplayName = $UserObj.displayName ; Description = $UserObj.description ; name = $UserObj.mailNickname }
37+ $ExoBulkRequests.Add (@ {
38+ CmdletInput = @ {
39+ CmdletName = ' Set-DistributionGroup'
40+ Parameters = $Params
41+ }
42+ })
43+ $ExoLogs.Add (@ {
44+ message = " Success - Edited group properties for $ ( $GroupName ) group. It might take some time to reflect the changes."
45+ target = $GroupId
46+ })
47+ } else {
48+ $PatchObj = @ {
49+ displayName = $UserObj.displayName
50+ description = $UserObj.description
51+ mailNickname = $UserObj.mailNickname
52+ mailEnabled = $OrgGroup.mailEnabled
53+ securityEnabled = $OrgGroup.securityEnabled
54+ }
55+ Write-Host " body: $ ( $PatchObj | ConvertTo-Json - Depth 10 - Compress) " - ForegroundColor Yellow
56+ if ($UserObj.membershipRules ) { $PatchObj | Add-Member - MemberType NoteProperty - Name ' membershipRule' - Value $UserObj.membershipRules - Force }
57+ try {
58+ $patch = New-GraphPOSTRequest - type PATCH - uri " https://graph.microsoft.com/beta/groups/$ ( $GroupId ) " - tenantid $UserObj.tenantFilter - body ($PatchObj | ConvertTo-Json - Depth 10 - Compress)
59+ $Results.Add (" Success - Edited group properties for $ ( $GroupName ) group. It might take some time to reflect the changes." )
60+ Write-LogMessage - headers $Headers - API $APIName - tenant $UserObj.tenantFilter - message " Edited group properties for $ ( $GroupName ) group" - Sev ' Info'
61+ } catch {
62+ $Results.Add (" Error - Failed to edit group properties: $ ( $_.Exception.Message ) " )
63+ Write-LogMessage - headers $Headers - API $APIName - tenant $UserObj.tenantFilter - message " Failed to patch group: $ ( $_.Exception.Message ) " - Sev ' Error'
64+ }
6265 }
6366 }
6467
@@ -73,7 +76,7 @@ function Invoke-EditGroup {
7376 }
7477
7578 if ($GroupType -eq ' Distribution List' -or $GroupType -eq ' Mail-Enabled Security' ) {
76- $Params = @ { Identity = $UserObj .groupId ; Member = $Member ; BypassSecurityGroupManagerCheck = $true }
79+ $Params = @ { Identity = $GroupId ; Member = $Member ; BypassSecurityGroupManagerCheck = $true }
7780 # Write-Host ($UserObj | ConvertTo-Json -Depth 10) #Debugging line
7881 $ExoBulkRequests.Add (@ {
7982 CmdletInput = @ {
@@ -94,7 +97,7 @@ function Invoke-EditGroup {
9497 $BulkRequests.Add (@ {
9598 id = " addMember-$Member "
9699 method = ' PATCH'
97- url = " groups/$ ( $UserObj .groupId ) "
100+ url = " groups/$ ( $GroupId ) "
98101 body = $AddMemberBody
99102 headers = @ {
100103 ' Content-Type' = ' application/json'
@@ -118,7 +121,7 @@ function Invoke-EditGroup {
118121 try {
119122 $Member = $_
120123 if ($GroupType -eq ' Distribution list' -or $GroupType -eq ' Mail-Enabled Security' ) {
121- $Params = @ { Identity = $UserObj .groupId ; Member = $Member.value ; BypassSecurityGroupManagerCheck = $true }
124+ $Params = @ { Identity = $GroupId ; Member = $Member.value ; BypassSecurityGroupManagerCheck = $true }
122125 $ExoBulkRequests.Add (@ {
123126 CmdletInput = @ {
124127 CmdletName = ' Add-DistributionGroupMember'
@@ -146,7 +149,7 @@ function Invoke-EditGroup {
146149 $Member = $_.value
147150 $MemberID = $_.addedFields.id
148151 if ($GroupType -eq ' Distribution list' -or $GroupType -eq ' Mail-Enabled Security' ) {
149- $Params = @ { Identity = $UserObj .groupId ; Member = $MemberID ; BypassSecurityGroupManagerCheck = $true }
152+ $Params = @ { Identity = $GroupId ; Member = $MemberID ; BypassSecurityGroupManagerCheck = $true }
150153 $ExoBulkRequests.Add (@ {
151154 CmdletInput = @ {
152155 CmdletName = ' Remove-DistributionGroupMember'
@@ -174,7 +177,7 @@ function Invoke-EditGroup {
174177 $Member = $_.value
175178 $MemberID = $_.addedFields.id
176179 if ($GroupType -eq ' Distribution list' -or $GroupType -eq ' Mail-Enabled Security' ) {
177- $Params = @ { Identity = $UserObj .groupId ; Member = $Member ; BypassSecurityGroupManagerCheck = $true }
180+ $Params = @ { Identity = $GroupId ; Member = $Member ; BypassSecurityGroupManagerCheck = $true }
178181 $ExoBulkRequests.Add (@ {
179182 CmdletInput = @ {
180183 CmdletName = ' Remove-DistributionGroupMember'
@@ -189,7 +192,7 @@ function Invoke-EditGroup {
189192 $BulkRequests.Add (@ {
190193 id = " removeMember-$Member "
191194 method = ' DELETE'
192- url = " groups/$ ( $UserObj .groupId ) /members/$MemberID /`$ ref"
195+ url = " groups/$ ( $GroupId ) /members/$MemberID /`$ ref"
193196 })
194197 $GraphLogs.Add (@ {
195198 message = " Removed member $Member from $ ( $GroupName ) group"
@@ -213,7 +216,7 @@ function Invoke-EditGroup {
213216 $BulkRequests.Add (@ {
214217 id = " addOwner-$Owner "
215218 method = ' POST'
216- url = " groups/$ ( $UserObj .groupId ) /owners/`$ ref"
219+ url = " groups/$ ( $GroupId ) /owners/`$ ref"
217220 body = @ {
218221 ' @odata.id' = $MemberODataBindString -f $ID
219222 }
@@ -241,7 +244,7 @@ function Invoke-EditGroup {
241244 $BulkRequests.Add (@ {
242245 id = " removeOwner-$ID "
243246 method = ' DELETE'
244- url = " groups/$ ( $UserObj .groupId ) /owners/$ID /`$ ref"
247+ url = " groups/$ ( $GroupId ) /owners/$ID /`$ ref"
245248 })
246249 $GraphLogs.Add (@ {
247250 message = " Removed $ ( $_.value ) from $ ( $GroupName ) group"
@@ -255,15 +258,15 @@ function Invoke-EditGroup {
255258 }
256259
257260 if ($GroupType -in @ ( ' Distribution List' , ' Mail-Enabled Security' ) -and ($AddOwners -or $RemoveOwners )) {
258- $CurrentOwners = New-ExoRequest - tenantid $TenantId - cmdlet ' Get-DistributionGroup' - cmdParams @ { Identity = $UserObj .groupId } - UseSystemMailbox $true | Select-Object - ExpandProperty ManagedBy
261+ $CurrentOwners = New-ExoRequest - tenantid $TenantId - cmdlet ' Get-DistributionGroup' - cmdParams @ { Identity = $GroupId } - UseSystemMailbox $true | Select-Object - ExpandProperty ManagedBy
259262
260263 $NewManagedBy = [system.collections.generic.list [string ]]::new()
261264 foreach ($CurrentOwner in $CurrentOwners ) {
262265 if ($RemoveOwners -and $RemoveOwners.addedFields.id -contains $CurrentOwner ) {
263266 $OwnerToRemove = $RemoveOwners | Where-Object { $_.addedFields.id -eq $CurrentOwner }
264267 $ExoLogs.Add (@ {
265268 message = " Removed owner $ ( $OwnerToRemove.label ) from $ ( $GroupName ) group"
266- target = $UserObj .groupId
269+ target = $GroupId
267270 })
268271 continue
269272 }
@@ -274,13 +277,13 @@ function Invoke-EditGroup {
274277 $NewManagedBy.Add ($NewOwner.addedFields.id )
275278 $ExoLogs.Add (@ {
276279 message = " Added owner $ ( $NewOwner.label ) to $ ( $GroupName ) group"
277- target = $UserObj .groupId
280+ target = $GroupId
278281 })
279282 }
280283 }
281284
282285 $NewManagedBy = $NewManagedBy | Sort-Object - Unique
283- $Params = @ { Identity = $UserObj .groupId ; ManagedBy = $NewManagedBy }
286+ $Params = @ { Identity = $GroupId ; ManagedBy = $NewManagedBy }
284287 $ExoBulkRequests.Add (@ {
285288 CmdletInput = @ {
286289 CmdletName = ' Set-DistributionGroup'
@@ -354,16 +357,16 @@ function Invoke-EditGroup {
354357
355358 if ($UserObj.sendCopies -eq $true ) {
356359 try {
357- $Params = @ { Identity = $UserObj .groupId ; subscriptionEnabled = $true ; AutoSubscribeNewMembers = $true }
360+ $Params = @ { Identity = $GroupId ; subscriptionEnabled = $true ; AutoSubscribeNewMembers = $true }
358361 New-ExoRequest - tenantid $TenantId - cmdlet ' Set-UnifiedGroup' - cmdParams $Params - useSystemMailbox $true
359362
360- $MemberParams = @ { Identity = $UserObj .groupId ; LinkType = ' members' }
363+ $MemberParams = @ { Identity = $GroupId ; LinkType = ' members' }
361364 $Members = New-ExoRequest - tenantid $TenantId - cmdlet ' Get-UnifiedGroupLinks' - cmdParams $MemberParams
362365
363366 $MemberSmtpAddresses = $Members | ForEach-Object { $_.PrimarySmtpAddress }
364367
365368 if ($MemberSmtpAddresses ) {
366- $subscriberParams = @ { Identity = $UserObj .groupId ; LinkType = ' subscribers' ; Links = @ ($MemberSmtpAddresses | Where-Object { $_ }) }
369+ $subscriberParams = @ { Identity = $GroupId ; LinkType = ' subscribers' ; Links = @ ($MemberSmtpAddresses | Where-Object { $_ }) }
367370 New-ExoRequest - tenantid $TenantId - cmdlet ' Add-UnifiedGroupLinks' - cmdParams $subscriberParams - Anchor $UserObj.mail
368371 }
369372
0 commit comments