FunctionsPublic/Set-GraphGroupDynamicMembership.ps1
<#
.SYNOPSIS Set dynamic membership properties for group .DESCRIPTION Set dynamic membership properties for group #> function Set-GraphGroupDynamicMembership { [cmdletbinding()] param ( [parameter(Mandatory=$true)][psobject]$accessToken, [parameter(Mandatory=$true)][string]$groupID, [parameter(Mandatory=$true)][bool]$dynamicGroup, [parameter(Mandatory=$false)][string]$dynamicMembershipRule ) end { if($accessToken.Length -eq 0 -or $groupID.Length -eq 0) { Write-Error "Invalid input received. Please specify all parameters in order to use this function." return $null } if($true -eq $dynamicGroup -and $dynamicMembershipRule.Length -eq 0) { Write-Error "Specify dynamic membership rule to set group to dynamic." return $null } $existingGroup = Get-GraphGroup -accessToken $accessToken -groupID $groupID if($null -eq $existingGroup) { Write-Error "Group not found. Please specify an existing group." return $null } $newGroupTypes = $existingGroup.groupTypes # # Remove dynamic grouptype if group shouldn't be dynamic # if($true -eq $newGroupTypes.Contains("DynamicMembership") -and $false -eq $dynamicGroup) { $newGroupTypes = $newGroupTypes | Where-Object {$_ -ne "DynamicMembership"} } # # Add dynamic grouptype if group should be dynamic # elseif ($false -eq $newGroupTypes.Contains("DynamicMembership") -and $true -eq $dynamicGroup) { $newGroupTypes += "DynamicMembership" } if($true -eq $dynamicGroup) { $sendBody = @{ "groupTypes" = $newGroupTypes; "membershipRule" = "$($dynamicMembershipRule)"; "membershipRuleProcessingState" = "On" } | ConvertTo-Json } else { $sendBody = @{ "groupTypes" = $newGroupTypes; "membershipRule" = ""; "membershipRuleProcessingState" = "Off" } | ConvertTo-Json } $responseBody = Invoke-RestMethod ` -Uri "https://graph.microsoft.com/v1.0/groups/$($groupID)" ` -Headers @{"Authorization" = "Bearer $($accessToken.AccessTokenCredential.GetNetworkCredential().password)"} ` -Body $sendBody ` -ContentType "application/json" ` -Method PATCH if($null -eq $responseBody) { return $null } else { return $responseBody.value } } } |