Advertisement
Guest User

CreateNewUserWithEmail.ps1

a guest
Feb 11th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # AUTHOR  : Ataur Rasool - Rasool.Ataur@gmail.com
  3. # DATE    : 02-05-2016
  4. # COMMENT : This script creates new Active Directory user, Emails the information to management
  5. # VERSION : 2
  6. ###########################################################
  7.  
  8. #What do we need?
  9. #FirstName
  10. #LastName
  11. #Verification
  12. #Password
  13. #Send email to management
  14. #Clear the script
  15.  
  16.  
  17. Write-Host "********************************************************************"
  18. Write-Host "**            New User Creation Script                         **"
  19. Write-Host "********************************************************************"
  20.  
  21. #----------------------------------------------------------
  22. #
  23. #Ask for the information required for the new UserAccount
  24. #----------------------------------------------------------
  25.  
  26. Do{
  27. #If any fields are blank - throw the following error > Write-Host "Field cannot be blank"
  28.  
  29. $NullName = "Field cannot be blank!"
  30.  
  31. $FirstName = Read-Host "Enter users's First Name"
  32. IF($FirstName -eq $NullName){$NullName}else{}
  33.  
  34. $LastName = Read-Host "Enter user's Last Name"
  35. IF($LastName -eq $NullName){$NullName}else{}
  36.  
  37. $UserName = Read-Host "Enter user ID (ie - arasool)"
  38. IF($UserName -eq $NullName){$NullName}else{}
  39.  
  40. $Password = Read-Host "Enter user's Password - Symbols are allowed"
  41. IF($Password -eq $NullName){$NullName}else{}
  42.  
  43.  
  44.  
  45. Write-Host "Checking if user already exists...."
  46.  
  47. }
  48. While($Null)
  49. #----------------------------------------------------------
  50. #
  51. #Check - Does this user already exist?
  52. #----------------------------------------------------------
  53. $Name = $UserName
  54. $AlreadyExists = "User exists! Please re-enter User ID"
  55. $DoesNotExist = "User does not exist"
  56.  
  57. Import-Module ActiveDirectory -ErrorAction SilentlyContinue
  58. $User = Get-ADUser -Filter {sAMAccountName -eq $Name} #Check AD for the username
  59. if ($User -eq $Null) {Write-Host $DoesNotExist}
  60. If ($User -eq $AlreadyExists) {Write-Host $AlreadyExists    -ForegroundColor Red }
  61. if ($User -eq $DoesNotExist ) {Write-Host $DoesNotExist     -ForegroundColor Red }
  62.  
  63. #----------------------------------------------------------
  64. #
  65. # Print the information on the screen, and confirm if the information is correct
  66. #----------------------------------------------------------
  67. Write-Host "`nFirst Name:`t`t$FirstName" -ForegroundColor Yellow
  68. Write-Host "Last Name:`t`t$LastName" -ForegroundColor Yellow
  69. Write-Host "UserID: `t`t$UserName" -ForegroundColor Yellow
  70. Write-Host "Password: `t`t$Password" -ForegroundColor Yellow
  71. $Answer = Read-Host "Is this information correct? (Y/N)"
  72. If($Answer.ToUpper() -eq "Y") {
  73. Write-Host "Creating" $UserName "now. Give this a few moments."
  74.  
  75. #----------------------------------------------------------
  76. #
  77. # LOAD ASSEMBLIES AND MODULES - Create User in ActiveDirectory
  78. #----------------------------------------------------------
  79. Import-Module ActiveDirectory -ErrorAction SilentlyContinue
  80. New-ADUser `
  81. -Name ($LastName+""+$FirstName.Substring(0,1)) `
  82. -SamAccountName $UserName `
  83. -DisplayName ($FirstName+""+$LastName) `
  84. -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) `
  85. -ChangePasswordAtLogon $False -Enabled $True `
  86. Add-ADGroupMember "Users" "Users";
  87. Set-ADUser -Identity $UserName -PasswordNeverExpires $true  #Don't forget the passwordneverexpires flag!
  88. Write-Host "Password never expires is now enabled"
  89. Start-Sleep -s 10 #Efficiency clock
  90.  
  91. #----------------------------------------------------------
  92. #
  93. # Email the users information to management
  94. #----------------------------------------------------------
  95. Write-Host "Emailing this information to management" -ForegroundColor Green #If the info is correct, email the information.
  96. #Email the info to management
  97. $PSEmailServer = "EMAIL SERVER"
  98. $Port = 587
  99. Send-MailMessage -From "SOMEONE@SOMEPLACE.COM" -To "SOMEONE@SOMEPLACE.COM" -Subject "Test email" -Body "New user account has been created. Please find the following information: Last Name:$LastName, First Name:$FirstName - UserID: $UserName - Password: $Password.    Please note that this information is automatically being sent to you. If there are any issues, please alert the IT department."
  100. Start-Sleep -s 5 #Efficiency clock
  101. Write-Host "Done!" -ForegroundColor Red #Done sending the email
  102.  
  103.  
  104.  
  105. if($Answer.ToUpper() -eq "N") {  Write-Host "Restart the script!" -ForegroundColor RED  #If the info is incorrect, have the user restart the script.
  106. Write-Host "This session will now be cleared in 10 seconds" -ForegroundColor Red
  107. Start-Sleep -s 10 #Efficiency clock
  108. cls
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement