Advertisement
Guest User

Untitled

a guest
Jul 25th, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.Read.All"
  2.  
  3. #Target Group ID
  4. $TargetGroupID = "GroupID"
  5. $TargetGroup = Get-MgGroup -GroupId $TargetGroupID
  6.  
  7.  
  8. #Extract groups from .yaml into a readable array
  9. $groups = Get-Content -Path "filepath.yaml" -Raw | ConvertFrom-Yaml
  10.  
  11.  
  12. #variable nulls
  13. $Yaml_Members_Source = $null
  14. $AAD_Members_Target = $null
  15. $fullGroup = $null
  16. $Yaml_Ids = $null
  17. $AAD_Ids = $null
  18. $Full_Details_Add = $null
  19. $Full_Details_Remove = $null
  20.  
  21.  
  22. #hashsets creation
  23. $Yaml_Members_Source = New-Object System.Collections.Generic.HashSet[string]
  24. $AAD_Members_Target = New-Object System.Collections.Generic.HashSet[string]
  25.  
  26.  
  27. #source hashset population
  28. foreach ($group in $groups) {
  29. $fullGroup = Get-MgGroup -Filter "displayName eq '$group'"
  30. [String[]]$Yaml_Ids = @(Get-MgGroupMember -GroupId $fullGroup.Id).Id
  31. $Yaml_Members_Source.UnionWith($Yaml_Ids)
  32. }
  33.  
  34.  
  35. #target hashset population
  36. [String[]]$AAD_Ids = @(Get-MgGroupMember -GroupId $TargetGroupID).Id
  37. $AAD_Members_Target.UnionWith($AAD_Ids)
  38.  
  39.  
  40. #Determine users to add and remove from Target
  41. $Users_To_Add = $Yaml_Members_Source.Where({$_ -notin $AAD_Members_Target})
  42. $Users_To_Remove = $AAD_Members_Target.Where({$_ -notin $Yaml_Members_Source})
  43.  
  44.  
  45. #Add users missing from Source to Target
  46. ForEach ($ADD_member in $Users_To_Add) {
  47.  
  48. try {
  49. $Full_Details_Add = Get-MgUser -UserID $ADD_member -ErrorAction:SilentlyContinue
  50. }
  51. catch {
  52. Write-error "User ""$($Full_Details_Add.UserPrincipalName)"" was not found"
  53. }
  54.  
  55. try {
  56. New-MgGroupMember -GroupId $TargetGroupID -DirectoryObjectId $ADD_member -ErrorAction:SilentlyContinue
  57. Write-Information "User ""$($Full_Details_Add.UserPrincipalName)"" added to ""$($TargetGroup.DisplayName)"""
  58. }
  59. catch {
  60. Write-Debug "User ""$($Full_Details_Add.UserPrincipalName)"" is already a member of ""$($TargetGroup.DisplayName)"""
  61. }
  62. }
  63.  
  64.  
  65. #Remove users that are in Target but not in the Source
  66. ForEach ($Remove_member in $Users_To_Remove) {
  67.  
  68. try {
  69. $Full_Details_Remove = Get-MgUser -UserID $Remove_member -ErrorAction:SilentlyContinue
  70. }
  71. catch {
  72. Write-error "User ""$($Full_Details_Remove.UserPrincipalName)"" was not found"
  73. }
  74.  
  75. Remove-MgGroupMemberByRef -GroupId $TargetGroupID -DirectoryObjectId $Remove_member
  76. Write-Information "User ""$($Full_Details_Remove.UserPrincipalName)"" removed from ""$($TargetGroup.DisplayName)"""
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement