Advertisement
PowerShell_PC_Aide

Add_Or_Remove_Users_to_multiple_Groups_26-03-2023.ps1

Mar 26th, 2023
1,198
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cls
  2. # Load the assembly to generate Windows Forms
  3. Add-type -AssemblyName System.Windows.Forms
  4.  
  5. # Create the GUI form
  6. $Form = New-Object System.Windows.Forms.Form
  7. $Form.Text = "Add/Remove ADUsers to/from ADGroups (v26-03-2023)"
  8. $Form.Size = New-Object System.Drawing.Size(650, 500)
  9.  
  10. # Create the Labels for User and Group Textboxes
  11. $UserListLabel = New-Object System.Windows.Forms.Label
  12. $UserListLabel.Location = New-Object System.Drawing.Point(25, 50)
  13. $UserListLabel.Width = 150
  14. $UserListLabel.Text = "User List :"
  15. $Form.Controls.Add($UserListLabel)
  16.  
  17. $GroupListLabel = New-Object System.Windows.Forms.Label
  18. $GroupListLabel.Location = New-Object System.Drawing.Point(325, 50)
  19. $GroupListLabel.Width = 150
  20. $GroupListLabel.Text = "Group List :"
  21. $Form.Controls.Add($GroupListLabel)
  22.  
  23. # Create the Textboxes for Users
  24. $UserTextbox = New-Object System.Windows.Forms.TextBox
  25. $UserTextbox.Location = New-Object System.Drawing.Point(25, 80)
  26. $UserTextbox.Width = 250
  27. $UserTextbox.Height = 210
  28. $UserTextbox.Multiline = $true
  29. $UserTextbox.ScrollBars = "Both"
  30. $UserTextbox.ForeColor = [System.Drawing.Color]::Gray
  31. # cue_banner
  32. $UserTextbox.Text = "Enter the UserPrincipalName and/or samAccountName per line"
  33.  
  34. $UserTextbox.Add_Enter({
  35.     if ($UserTextbox.Text -eq "Enter the UserPrincipalName and/or samAccountName per line") {
  36.         $UserTextbox.Text = ""
  37.         $UserTextbox.ForeColor = [System.Drawing.Color]::Black
  38.     }
  39. })
  40.  
  41. $UserTextbox.Add_Leave({
  42.     if ([string]::IsNullOrEmpty($UserTextbox.Text)) {
  43.         $UserTextbox.ForeColor = [System.Drawing.Color]::Gray
  44.         $UserTextbox.Text = "Enter the UserPrincipalName and/or samAccountName per line"
  45.     }
  46. })
  47.  
  48. $Form.Controls.Add($UserTextbox)
  49.  
  50. # Add error label for UerText
  51. $UserErrorLabel = New-Object System.Windows.Forms.Label
  52. $UserErrorLabel.Location = New-Object System.Drawing.Point(25, 311)
  53. $UserErrorLabel.Width = 100
  54. $UserErrorLabel.Height = 20
  55. $UserErrorLabel.Text = "Error :"
  56. $Form.Controls.Add($UserErrorLabel)
  57.  
  58. # Add real-time validation for UserTextbox
  59. $UserErrorRichTextbox = New-Object System.Windows.Forms.RichTextBox
  60. $UserErrorRichTextbox.Location = New-Object System.Drawing.Point(25, 330)
  61. $UserErrorRichTextbox.Width = 250
  62. $UserErrorRichTextbox.Height = 60
  63. $UserErrorRichTextbox.ReadOnly = $true
  64. $UserErrorRichTextbox.ScrollBars = "Vertical"
  65. $Form.Controls.Add($UserErrorRichTextbox)
  66.  
  67. $UserTextbox.Add_TextChanged({
  68.         $UserErrorRichTextbox.Clear()
  69.         $Users = $UserTextbox.Text -split "`r`n" | Where-Object { $_ -ne "" }
  70.         foreach ($User in $Users) {
  71.             $UserAccount = Get-ADUser -Filter { UserPrincipalName -eq $User -or SamAccountName -eq $User } -ErrorAction SilentlyContinue
  72.             if (!$UserAccount) {
  73.                 $UserErrorRichTextbox.SelectionColor = [System.Drawing.Color]::Red
  74.                 $UserErrorRichTextbox.AppendText("User '$User' does not exist.`n")
  75.             }
  76.         }
  77.     })
  78.  
  79. # Create the Textboxes for Groups
  80. $GroupTextbox = New-Object System.Windows.Forms.TextBox
  81. $GroupTextbox.Location = New-Object System.Drawing.Point(325, 80)
  82. $GroupTextbox.Width = 270
  83. $GroupTextbox.Height = 210
  84. $GroupTextbox.Multiline = $true
  85. $GroupTextbox.ScrollBars = "Both"
  86. $GroupTextbox.ForeColor = [System.Drawing.Color]::Gray
  87. # cue_banner
  88. $GroupTextbox.Text = "Enter the ADGroup Name per line"
  89.  
  90. $GroupTextbox.Add_Enter({
  91.     if ($GroupTextbox.Text -eq "Enter the ADGroup Name per line") {
  92.         $GroupTextbox.Text = ""
  93.         $GroupTextbox.ForeColor = [System.Drawing.Color]::Black
  94.     }
  95. })
  96.  
  97. $GroupTextbox.Add_Leave({
  98.     if ([string]::IsNullOrEmpty($GroupTextbox.Text)) {
  99.         $GroupTextbox.ForeColor = [System.Drawing.Color]::Gray
  100.         $GroupTextbox.Text = "Enter the ADGroup Name per line"
  101.     }
  102. })
  103.  
  104. $Form.Controls.Add($GroupTextbox)
  105.  
  106. # Add error label for groupText
  107. $GroupErrorLabel = New-Object System.Windows.Forms.Label
  108. $GroupErrorLabel.Location = New-Object System.Drawing.Point(325, 311)
  109. $GroupErrorLabel.Width = 100
  110. $GroupErrorLabel.Height = 20
  111. $GroupErrorLabel.Text = "Error :"
  112. $Form.Controls.Add($GroupErrorLabel)
  113.  
  114. # Add real-time validation for GroupTextbox
  115. $GroupErrorRichTextbox = New-Object System.Windows.Forms.RichTextBox
  116. $GroupErrorRichTextbox.Location = New-Object System.Drawing.Point(325, 330)
  117. $GroupErrorRichTextbox.Width = 250
  118. $GroupErrorRichTextbox.Height = 60
  119. $GroupErrorRichTextbox.ReadOnly = $true
  120. $GroupErrorRichTextbox.ScrollBars = "Vertical"
  121. $Form.Controls.Add($GroupErrorRichTextbox)
  122.  
  123. $GroupTextbox.Add_TextChanged({
  124.         $GroupErrorRichTextbox.Clear()
  125.         $Groups = $GroupTextbox.Text -split "`r`n" | Where-Object { $_ -ne "" }
  126.         foreach ($Group in $Groups) {
  127.             $GroupAccount = Get-ADGroup -Filter { Name -eq $Group } -ErrorAction SilentlyContinue
  128.             if (!$GroupAccount) {
  129.                 $GroupErrorRichTextbox.SelectionColor = [System.Drawing.Color]::Red
  130.                 $GroupErrorRichTextbox.AppendText("Group '$Group' does not exist.`n")
  131.             }
  132.         }
  133.     })
  134.  
  135.  
  136. # Create the Radio Button for selecting Add or Remove
  137. $ActionGroupBox = New-Object System.Windows.Forms.GroupBox
  138. $ActionGroupBox.Location = New-Object System.Drawing.Point(25, 400)
  139. $ActionGroupBox.Size = New-Object System.Drawing.Size(165, 50)
  140. $ActionGroupBox.Text = "Action"
  141. $Form.Controls.Add($ActionGroupBox)
  142.  
  143. $AddRadioButton = New-Object System.Windows.Forms.RadioButton
  144. $AddRadioButton.Location = New-Object System.Drawing.Point(10, 20)
  145. $AddRadioButton.Size = New-Object System.Drawing.Size(90, 20)
  146. $AddRadioButton.Text = "Add"
  147. $AddRadioButton.Checked = $true
  148. $ActionGroupBox.Controls.Add($AddRadioButton)
  149.  
  150. $RemoveRadioButton = New-Object System.Windows.Forms.RadioButton
  151. $RemoveRadioButton.Location = New-Object System.Drawing.Point(100, 20)
  152. $RemoveRadioButton.Size = New-Object System.Drawing.Size(80, 20)
  153. $RemoveRadioButton.Text = "Remove"
  154. $ActionGroupBox.Controls.Add($RemoveRadioButton)
  155.  
  156. # Create Button OK
  157. $OKButton = New-Object System.Windows.Forms.Button
  158. $OKButton.Location = New-Object System.Drawing.Point(250, 420)
  159. $OKButton.Size = New-Object System.Drawing.Size(75, 23)
  160. $OKButton.Text = "OK"
  161. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  162. $Form.AcceptButton = $OKButton
  163. $Form.Controls.Add($OKButton)
  164.  
  165. # Create Button Cancel
  166. $CancelButton = New-Object System.Windows.Forms.Button
  167. $CancelButton.Location = New-Object System.Drawing.Point(350, 420)
  168. $CancelButton.Size = New-Object System.Drawing.Size(75, 23)
  169. $CancelButton.Text = "Cancel"
  170. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  171. $Form.CancelButton = $CancelButton
  172. $Form.Controls.Add($CancelButton)
  173.  
  174. # Display the form
  175. if ($Form.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
  176.     $Users = $UserTextbox.Text -split "`r`n" | Where-Object { $_ -ne "" }
  177.     $Groups = $GroupTextbox.Text -split "`r`n" | Where-Object { $_ -ne "" }
  178.     $Action = "Add"
  179.  
  180.     if ($RemoveRadioButton.Checked) {
  181.         $Action = "Remove"
  182.     }
  183.  
  184.     $Log = @()
  185.     foreach ($Group in $Groups) {
  186.         if (!(Get-ADGroup -Filter { Name -eq $Group })) {
  187.             $LogEntry = [PSCustomObject]@{
  188.                 Users   = ""
  189.                 Groups  = $Group
  190.                 Action  = $Action
  191.                 Error   = "$Group does not exist"
  192.                 Warning = ""
  193.             }
  194.             $LogEntry | Format-Table -AutoSize
  195.             $Log += $LogEntry
  196.             continue
  197.         }
  198.         foreach ($User in $Users) {
  199.             $UserAccount = $null
  200.             try {
  201.                 $UserAccount = Get-ADUser -Filter { UserPrincipalName -eq $User } -ErrorAction Stop
  202.             }
  203.             catch {
  204.                 Write-Verbose "User with UPN '$User' not found. Trying with SamAccountName."
  205.             }
  206.  
  207.             if (!$UserAccount) {
  208.                 $UserAccount = Get-ADUser -Filter { SamAccountName -eq $User } -ErrorAction SilentlyContinue
  209.             }
  210.  
  211.             if (!$UserAccount) {
  212.                 $LogEntry = [PSCustomObject]@{
  213.                     Users   = $User
  214.                     Groups  = ""
  215.                     Action  = $Action
  216.                     Error   = "$User does not exist"
  217.                     Warning = ""
  218.                 }
  219.                 $LogEntry | Format-Table -AutoSize
  220.                 $Log += $LogEntry
  221.                 continue
  222.             }
  223.             else {
  224.                 $UserName = $UserAccount.SamAccountName
  225.             }
  226.  
  227.             $PDC = Get-ADDomain | Select -ExpandProperty PDCEmulator
  228.             if ($Action -eq "Add") {
  229.                 if (Get-ADGroupMember -Identity $Group | Where-Object { $_.SamAccountName -eq $UserName }) {
  230.                     $LogEntry = [PSCustomObject]@{
  231.                         Users   = $User
  232.                         Groups  = $Group
  233.                         Action  = $Action
  234.                         Error   = ""
  235.                         Warning = "$User is already a member of $Group"
  236.                         Ajout   = ""
  237.                     }
  238.                     $Log += $LogEntry
  239.                 }
  240.                 else {
  241.                     try {
  242.                         Add-ADGroupMember -Identity $Group -Members $UserName -Server $PDC -ErrorAction Stop
  243.                         $LogEntry = [PSCustomObject]@{
  244.                             Users   = $User
  245.                             Groups  = $Group
  246.                             Action  = $Action
  247.                             Error   = ""
  248.                             Warning = ""
  249.                             Ajout   = "Added"
  250.                         }
  251.                         $LogEntry | Format-Table -AutoSize
  252.                         $Log += $LogEntry
  253.                     }
  254.                     catch {
  255.                         $LogEntry = [PSCustomObject]@{
  256.                             Users   = $User
  257.                             Groups  = $Group
  258.                             Action  = $Action
  259.                             Error   = "Error adding $User to $Group $($PSItem.Exception.Message)"
  260.                             Warning = ""
  261.                             Ajout   = ""
  262.                         }
  263.                         $LogEntry | Format-Table -AutoSize
  264.                         $Log += $LogEntry
  265.                     }
  266.                 }
  267.             }
  268.             elseif ($Action -eq "Remove") {
  269.                 if (!(Get-ADGroupMember -Identity $Group | Where-Object { $_.SamAccountName -eq $UserName })) {
  270.                     $LogEntry =
  271.                     [PSCustomObject]@{
  272.                         Users   = $User
  273.                         Groups  = $Group
  274.                         Action  = $Action
  275.                         Error   = ""
  276.                         Warning = "$User is not a member of $Group"
  277.                         Removal = ""
  278.                     }
  279.                     $LogEntry | Format-Table -AutoSize
  280.                     $Log += $LogEntry
  281.                 }
  282.                 else {
  283.                     try {
  284.                         $UserIsMember = Get-ADGroupMember -Identity $Group -Server $PDC | Where-Object { $_.SamAccountName -eq $UserName }
  285.  
  286.                         if ($UserIsMember) {
  287.                             Remove-ADGroupMember -Identity $Group -Members $UserName -Confirm:$false -Server $PDC -ErrorAction Stop
  288.                             $LogEntry = [PSCustomObject]@{
  289.                                 Users   = $User
  290.                                 Groups  = $Group
  291.                                 Action  = $Action
  292.                                 Error   = ""
  293.                                 Warning = ""
  294.                                 Removal = "Removed"
  295.                             }
  296.                             $LogEntry | Format-Table -AutoSize
  297.                             $Log += $LogEntry
  298.                         }
  299.                         else {
  300.                             $LogEntry = [PSCustomObject]@{
  301.                                 Users   = $User
  302.                                 Groups  = $Group
  303.                                 Action  = $Action
  304.                                 Error   = ""
  305.                                 Warning = "$User is not a member of $Group"
  306.                                 Removal = ""
  307.                             }
  308.                             $LogEntry | Format-Table -AutoSize
  309.                             $Log += $LogEntry
  310.                         }
  311.                     }
  312.                     catch {
  313.                         $LogEntry = [PSCustomObject]@{
  314.                             Users   = $User
  315.                             Groups  = $Group
  316.                             Action  = $Action
  317.                             Error   = "Error removing $User from $Group $($PSItem.Exception.Message)"
  318.                             Warning = ""
  319.                             Removal = ""
  320.                         }
  321.                         $LogEntry | Format-Table -AutoSize
  322.                         $Log += $LogEntry
  323.                     }
  324.                 }
  325.             }
  326.  
  327.         }
  328.     }
  329.  
  330.     $TimeStamp = Get-Date -Format "dd-MM-yyyy-HH-mm-ss"
  331.     $Log | Export-Csv -Path "C:\temp\Rapport_Add_or_Remove_ADUsers_to_mul_ADGroups_$TimeStamp.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ";" -Force -Append
  332.  
  333.     # Afficher en rouge les erreurs, en vert les ajouts ou les retraits, en jaune les warnings dans la console PowerShell
  334.     $Log | Where-Object { $_.Error -ne "" } | ForEach-Object {
  335.         Write-Host $_ -ForegroundColor Red
  336.     }
  337.  
  338.     $Log | Where-Object { $_.Ajout -eq "Added" } | ForEach-Object {
  339.         Write-Host $_ -ForegroundColor Green
  340.     }
  341.  
  342.     $Log | Where-Object { $_.Warning -ne "" } | ForEach-Object {
  343.         Write-Host $_ -ForegroundColor Yellow
  344.     }
  345.  
  346.     $Log | Where-Object { $_.Removal -eq "Removed" } | ForEach-Object {
  347.         Write-Host $_ -ForegroundColor Green
  348.     }
  349. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement