Advertisement
Guest User

Administration

a guest
Feb 21st, 2024
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. -------------------------------- administration script from CSV
  2.  
  3.  
  4. [CmdletBinding()]
  5. param (
  6. [Parameter(
  7. Mandatory = $true,
  8. HelpMessage = "Group name"
  9. )]
  10. [string] $groupName = "",
  11. [Parameter(
  12. Mandatory = $true,
  13. HelpMessage = "Path to CSV file"
  14. )]
  15. [string] $path = "",
  16. [Parameter(
  17. Mandatory = $false,
  18. HelpMessage = "CSV file delimiter"
  19. )]
  20. [string] $delimiter = ",",
  21. [Parameter(
  22. Mandatory = $false,
  23. HelpMessage = "Find users on DisplayName, Email or UserPrincipalName"
  24. )]
  25. [ValidateSet("DisplayName", "Email", "UserPrincipalName")]
  26. [string] $filter = "DisplayName"
  27. )
  28. Function Add-UsersToGroup {
  29. <#
  30. .SYNOPSIS
  31. Get users from the requested DN
  32. #>
  33. process{
  34. # Import the CSV File
  35. $users = (Import-Csv -Path $path -Delimiter $delimiter -header "name").name
  36. # Find the users in the Active Directory
  37. $users | ForEach {
  38. $user = Get-ADUser -filter "$filter -eq '$_'" | Select ObjectGUID
  39. if ($user) {
  40. Add-ADGroupMember -Identity $groupName -Members $user
  41. Write-Host "$_ added to the group"
  42. }else {
  43. Write-Warning "$_ not found in the Active Directory"
  44. }
  45. }
  46. }
  47. }
  48. # Load the Active Directory Module
  49. Import-Module -Name ActiveDirectory
  50. # Add user from CSV to given Group
  51. Add-UsersToGroup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement