Advertisement
Guest User

NewUser.ps1

a guest
Feb 9th, 2016
236
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. #----------------------------------------------------------
  31. #
  32. #Ask for the information required for the new UserAccount
  33. #----------------------------------------------------------
  34.  
  35. $FirstName = Read-Host "Enter users's First Name"
  36.  
  37. $LastName = Read-Host "Enter user's Last Name"
  38.  
  39. $UserName = Read-Host "Enter user ID (ie - arasool)"
  40.  
  41. $Password = Read-Host "Enter user's Password - Symbols are allowed"
  42.  
  43. Write-Host "Checking if user already exists - Please give this time."
  44.  
  45.  
  46. #----------------------------------------------------------
  47. #
  48. #Note: A progress bar would be nice here!
  49. #----------------------------------------------------------
  50.  
  51.  
  52. #----------------------------------------------------------
  53. #
  54. # Check - Does this user already exist?
  55. #----------------------------------------------------------
  56.  
  57.  
  58. strUserName = $UserName
  59. dtStart = TimeValue(Now())
  60. Set objConnection = CreateObject("ADODB.Connection")
  61. objConnection.Open "Provider=ADsDSOObject;"
  62.  
  63. Set objCommand = CreateObject("ADODB.Command")
  64. objCommand.ActiveConnection = objConnection
  65.  
  66. objCommand.CommandText = _
  67.     "PDC03;(&(objectCategory=User)" & _
  68.          "(samAccountName=" & strUserName & "));samAccountName;subtree"
  69.  
  70. Set objRecordSet = objCommand.Execute
  71.  
  72. If objRecordset.RecordCount = 0 Then
  73.     WScript.Echo "sAMAccountName: " & strUserName & " does not exist."
  74. Else
  75.     WScript.Echo strUserName & " exists."
  76. End If
  77.  
  78. objConnection.Close
  79.  
  80. #----------------------------------------------------------
  81. #
  82. # Go to sleep - Allows time in between the script to be efficient, and parse no errors.
  83. #----------------------------------------------------------
  84.  
  85. Start-Sleep -s 5
  86.  
  87.  
  88. #----------------------------------------------------------
  89. #
  90. # Print the information on the screen, and confirm if the information is correct
  91. #----------------------------------------------------------
  92.  
  93. Write-Host "`nFirst Name:`t`t$FirstName" -ForegroundColor Yellow
  94.  
  95. Write-Host "Last Name:`t`t$LastName" -ForegroundColor Yellow
  96.  
  97. Write-Host "UserID: `t`t$UserName" -ForegroundColor Yellow
  98.  
  99. Write-Host "Password: `t`t$Password" -ForegroundColor Yellow
  100.  
  101. $Answer = Read-Host "Is this the information you want to use? (Y/N)"
  102. If ($Answer.ToUpper() -ne "Y")
  103. { Write-Host "`n`nOK.  Restart the script...`n" -ForegroundColor RED
  104. break
  105. }
  106.  
  107.  
  108. #----------------------------------------------------------
  109. #
  110. #If answer is "n" - go back to the beginning.
  111. #----------------------------------------------------------
  112.  
  113.  
  114.  
  115.  
  116. #----------------------------------------------------------
  117. #
  118. #Create the user account
  119. #----------------------------------------------------------
  120.  
  121. Set objOU = GetObject("LDAP://OU=management,dc=fabrikam,dc=com")
  122.  
  123. Set objUser = objOU.Create("User", "cn=$UserName")
  124. objUser.Put "sAMAccountName", "$UserName"
  125. objUser.SetInfo
  126.  
  127. #----------------------------------------------------------
  128. #
  129. #Password does not expire flag
  130. #----------------------------------------------------------
  131.  
  132. Set-ADUser -Identity $UserName -PasswordNeverExpires $true
  133. WScript.Echo "Password never expires is now enabled"
  134.  
  135. Write-Host "The following username, " $UserName ",has been created." -ForegroundColor Green
  136.  
  137.  
  138.  
  139. #----------------------------------------------------------
  140. #
  141. #Script will now close message
  142. #----------------------------------------------------------
  143.  
  144. write-host "This window will close in 3 seconds"
  145.  
  146.  
  147. Start-Sleep -s 3
  148.  
  149. cls
  150. stop-process -Id $PID
  151. Break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement