Advertisement
Guest User

NewUser.ps1

a guest
Feb 9th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # AUTHOR  : Ataur Rasool - IT Dept
  3. # DATE    : 02-05-2016
  4. # COMMENT : This script creates new Active Directory user On PDC03
  5. # VERSION : 2
  6. ###########################################################
  7.  
  8. #What do we need?
  9. #Domain DNS suffix
  10. #PDC02 AD details - v2
  11. #PDC03 AD details - v3
  12. #FirstName
  13. #LastName
  14. #Verification
  15. #Password
  16. #Loop
  17.  
  18. #----------------------------------------------------------
  19. #
  20. # LOAD ASSEMBLIES AND MODULES
  21. #----------------------------------------------------------
  22.  
  23.  
  24. Import-Module ActiveDirectory -ErrorAction SilentlyContinue
  25.  
  26. Write-Host "********************************************************************"
  27. Write-Host "**            New User Creation Script                        **"
  28. Write-Host "********************************************************************"
  29.  
  30. #Ask for the information required for the new UserAccount
  31.  
  32. $FirstName = Read-Host "Enter users's First Name"
  33.  
  34. $LastName = Read-Host "Enter user's Last Name"
  35.  
  36. $UserName = Read-Host "Enter user ID (ie - arasool)"
  37.  
  38. $Password = Read-Host "Enter user's Password - Symbols are allowed"
  39.  
  40.  
  41. Write-Host "Checking if user already exists - Please give this time."
  42.  
  43. # Go to sleep - Allows time in between the script to be efficient, and parse no errors.
  44. Start-Sleep -s 5
  45.  
  46. # Check - Does this user already exist?
  47.  
  48.  
  49. strUserName = $UserName
  50. dtStart = TimeValue(Now())
  51. Set objConnection = CreateObject("ADODB.Connection")
  52. objConnection.Open "Provider=ADsDSOObject;"
  53.  
  54. Set objCommand = CreateObject("ADODB.Command")
  55. objCommand.ActiveConnection = objConnection
  56.  
  57. objCommand.CommandText = _
  58.     "PDC03;(&(objectCategory=User)" & _
  59.          "(samAccountName=" & strUserName & "));samAccountName;subtree"
  60.  
  61. Set objRecordSet = objCommand.Execute
  62.  
  63. If objRecordset.RecordCount = 0 Then
  64.     WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
  65. Else
  66.     WScript.Echo strUserName & " exists."
  67. End If
  68.  
  69. objConnection.Close
  70.  
  71.  
  72.  
  73. # Print the information on the screen, and confirm if the information is correct
  74.  
  75. Write-Host "`nFirst Name:`t`t$FirstName" -ForegroundColor Yellow
  76.  
  77. Write-Host "Last Name:`t`t$LastName" -ForegroundColor Yellow
  78.  
  79. Write-Host "UserID: `t`t$UserName" -ForegroundColor Yellow
  80.  
  81. Write-Host "Password: `t`t$Password" -ForegroundColor Yellow
  82.  
  83. $Answer = Read-Host "Is this the information you want to use? (Y/N)"
  84. If ($Answer.ToUpper() -ne "Y")
  85. { Write-Host "`n`nOK.  Restart the script...`n" -ForegroundColor RED
  86. break
  87. }
  88. #If answer is "n" - go back to the beginning.
  89.  
  90.  
  91. #Create the user account
  92.  
  93. Set objOU = GetObject("LDAP://OU=management,dc=fabrikam,dc=com")
  94.  
  95. Set objUser = objOU.Create("User", "cn=$UserName")
  96. objUser.Put "sAMAccountName", $UserName
  97. objUser.SetInfo
  98. #Password does not expire
  99. Set-ADUser -Identity $UserName -PasswordNeverExpires $true
  100. WScript.Echo "Password never expires is now enabled"
  101.  
  102. Write-Host "The following username, " $UserName ",has been created." -ForegroundColor Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement