Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. [CmdletBinding()]
  2.  
  3. param (
  4.  
  5. $Path = "C:\Users\Administrator\Desktop\users.csv",
  6.  
  7. $Domain = "a18zagbi.local",
  8.  
  9. $Password = "Syp9393"
  10.  
  11. ) # params
  12.  
  13.  
  14. # Import Active Directory module
  15. Import-Module ActiveDirectory
  16. try {
  17. $users = Import-Csv -Encoding UTF7 -Delimiter ";" -Path $Path
  18. }
  19. catch {
  20. Write-Error $_
  21. } # try catch file
  22.  
  23.  
  24. $defpassword = (convertto-securestring $Password -AsPlainText -Force)
  25.  
  26. # Create users
  27. foreach ($user in $users) {
  28. $ADUser = $null
  29. $domain = $Domain
  30. $name = $user.Name
  31. $login = $user.Login
  32. # $department = $user.Department
  33. $email = $user.Email
  34. $description = $user.Description
  35.  
  36. If (Get-AdUser -F {(SamAccountName -eq $login) -and (enabled -eq $true)}) {
  37. Write-Output "there following user exists $login and is enabled"
  38. # should break
  39. }
  40. elseif (Get-AdUser -F {(SamAccountName -eq $login) -and (enabled -eq $False)}) {
  41. Write-Output "Please enable the following user $login"
  42. # should break
  43.  
  44. } # elseif
  45.  
  46. # Departments OU
  47. switch ($user.Department) {
  48. "RND" { $OUPath = "OU=RND,DC=a18zagbi,DC=local" } # Modify with the correct path
  49. "IT" { $OUPath = "OU=IT,DC=a18zagbi,DC=local" } # Modify with the correct path
  50. "Executives" { $OUPath = "OU=Executives,DC=a18zagbi,DC=local" } # Modify with the correct path
  51. "HR" { $OUPath = "OU=HR,DC=a18zagbi,DC=local" } # Modify with the correct path
  52. "SALES" { $OUPath = "OU=sales,DC=a18zagbi,DC=local" } # Modify with the correct path
  53. Default { $OUPath = "OU=myusers,DC=a18zagbi,DC=local" }
  54. }
  55.  
  56. $ADUserProperty = @{
  57. SamAccountName = $Login
  58. Name = $Name
  59. EmailAddress = $email
  60. Description = $description
  61. UserPrincipalName = $login
  62. DisplayName = $Name
  63. Enabled = $True
  64. Server = $Domain
  65. AccountPassword = $defpassword
  66. $Path = $OUPath
  67. } # Hashtable for splatting
  68.  
  69. # Create user
  70. Write-Verbose "Creating user $Login"
  71. try {
  72. New-ADuser @ADUserProperty -ErrorAction Stop
  73. }
  74. catch {
  75. write-error $_
  76. } # try catch
  77. } # foreach
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement