Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Import-Module ActiveDirectory
  2. $path = Split-Path -Parent $MyInvocation.MyCommand.Definition
  3. $newpath = $path + "import_users.csv"
  4. # Define variables
  5. $log = $path + "created_ActiveDirectory_users.log"
  6. $date = Get-Date
  7. $i = 0
  8.  
  9. function createActiveDirectoryUsers {
  10. "Created the following Active Directory users (on " + $date + "): " | Out-File $log -Append
  11. "--------------------------------------------" | Out-File $log -Append
  12.  
  13. Import-Csv $newpath | ForEach-Object {
  14. $samAccount = $_.SamAccountName
  15. try {
  16. $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)"
  17. } catch { }
  18. if (!$exists) {
  19. $i++
  20. # Set all variables according to the table names in the Excel
  21. # sheet / import CSV. The names can differ in every project, but
  22. # if the names change, make sure to change it below as well.
  23. $setpass = ConvertTo-SecureString -AsPlainText $_.Password -Force
  24. New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
  25. -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
  26. -Description $_.Description -EmailAddress $_.eMail `
  27. -StreetAddress $_.StreetAddress -City $_.L `
  28. -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
  29. -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
  30. -OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU
  31.  
  32. $output = $i.ToString() + ") Name: " + $_.CN + " sAMAccountName: "
  33. $output += $sam + " Pass: " + $_.Password
  34. $output | Out-File $log -append
  35. } else {
  36. "SKIPPED - USER ALREADY EXISTS OR ERROR: " + $_.CN | Out-File $log -append
  37. }
  38. }
  39. "----------------------------------------" + "`n" | Out-File $log -append
  40. }
  41.  
  42. createActiveDirectoryUsers
  43.  
  44. $_.DisplayName
  45.  
  46. #Test to make sure your output looks correct
  47. #You can do this by running the following:
  48. Import-csv ".import_create_ad_users.csv" | Out-GridView
  49.  
  50.  
  51.  
  52. # ERROR REPORTING ALL
  53. # When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
  54. Set-StrictMode -Version latest
  55.  
  56. Import-Module ActiveDirectory
  57. $path = Split-Path -Parent $MyInvocation.MyCommand.Definition
  58. $newpath = $path + "import_users.csv"
  59. # Define variables
  60. $log = $path + "created_ActiveDirectory_users.log"
  61. $date = Get-Date
  62. $i = 0
  63.  
  64. Function createActiveDirectoryUsers {
  65. "Created the following Active Directory users (on " + $date + "): " | Out-File $log -Append
  66. "--------------------------------------------" | Out-File $log -Append
  67. #Import CSV
  68. Import-Csv $newpath | ForEach-Object {
  69.  
  70. # Check to see if SamAccount exists
  71. $samAccount = $_.SamAccountName
  72. try {
  73. $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)"
  74. } catch { }
  75. If(!$exists){
  76. #Convert Password to Secure String
  77. $setpass = ConvertTo-SecureString -AsPlainText $_.Password -Force
  78. Try
  79. {
  80.  
  81. New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
  82. -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
  83. -Description $_.Description -EmailAddress $_.eMail `
  84. -StreetAddress $_.StreetAddress -City $_.L `
  85. -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
  86. -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
  87. -OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU
  88.  
  89. $dn = (Get-ADUser $_.SamAccountName).DistinguishedName
  90.  
  91. # Rename the object to a good looking name
  92. $newdn = (Get-ADUser $_.SamAccountName).DistinguishedName
  93. Rename-ADObject -Identity $newdn -NewName ($_.GivenName + " " + $_.Initials + " "+ $_.SN)
  94.  
  95.  
  96. #Create Log
  97. "[INFORMATION]`t Renamed the user $($_.SamAccountName) to $($_.GivenName) $($_.SN)`r`n" | Out-File $log -append
  98. "[INFORMATION]`t Created new user named: $($_.SamAccountName)" | Out-File $log -append
  99. "[INFORMATION]`t Password for new user: $($_.Password)" | Out-File $log -append
  100.  
  101. }
  102.  
  103. Catch
  104. {
  105. # Error if something with the user was broken
  106. Write-Host "[ERROR]`t Oops, something went wrong: $($_.Exception.Message)`r`n"
  107. }
  108. }
  109. Else
  110. {
  111. Write-Host "[SKIPPED]`t User $($_.SamAccountName) ($($_.GivenName) $($_.SN)) already exists or returned an error!`r`n"
  112. "[SKIPPED]`t User $($_.SamAccountName) ($($_.GivenName) $($_.SN)) already exists or returned an error!" | Out-File $log -append
  113. }
  114. $i++
  115. }
  116. "Processing ended (on " + $date + "): " | Out-File $log -append
  117. "--------------------------------------------" + "`r`n" | Out-File $log -append
  118. }
  119.  
  120. createActiveDirectoryUsers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement