Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###########################################################
- # AUTHOR : Ataur Rasool - Rasool.Ataur@gmail.com
- # DATE : 02-05-2016
- # COMMENT : This script creates new Active Directory user, Emails the information to management
- # VERSION : 2
- ###########################################################
- #What do we need?
- #FirstName
- #LastName
- #Verification
- #Password
- #Send email to management
- #Clear the script
- Write-Host "********************************************************************"
- Write-Host "** New User Creation Script **"
- Write-Host "********************************************************************"
- #----------------------------------------------------------
- #
- #Ask for the information required for the new UserAccount
- #----------------------------------------------------------
- Do{
- #If any fields are blank - throw the following error > Write-Host "Field cannot be blank"
- $NullName = "Field cannot be blank!"
- $FirstName = Read-Host "Enter users's First Name"
- IF($FirstName -eq $NullName){$NullName}else{}
- $LastName = Read-Host "Enter user's Last Name"
- IF($LastName -eq $NullName){$NullName}else{}
- $UserName = Read-Host "Enter user ID (ie - arasool)"
- IF($UserName -eq $NullName){$NullName}else{}
- $Password = Read-Host "Enter user's Password - Symbols are allowed"
- IF($Password -eq $NullName){$NullName}else{}
- Write-Host "Checking if user already exists...."
- }
- While($Null)
- #----------------------------------------------------------
- #
- #Check - Does this user already exist?
- #----------------------------------------------------------
- $Name = $UserName
- $AlreadyExists = "User exists! Please re-enter User ID"
- $DoesNotExist = "User does not exist"
- Import-Module ActiveDirectory -ErrorAction SilentlyContinue
- $User = Get-ADUser -Filter {sAMAccountName -eq $Name} #Check AD for the username
- if ($User -eq $Null) {Write-Host $DoesNotExist}
- If ($User -eq $AlreadyExists) {Write-Host $AlreadyExists -ForegroundColor Red }
- if ($User -eq $DoesNotExist ) {Write-Host $DoesNotExist -ForegroundColor Red }
- #----------------------------------------------------------
- #
- # Print the information on the screen, and confirm if the information is correct
- #----------------------------------------------------------
- Write-Host "`nFirst Name:`t`t$FirstName" -ForegroundColor Yellow
- Write-Host "Last Name:`t`t$LastName" -ForegroundColor Yellow
- Write-Host "UserID: `t`t$UserName" -ForegroundColor Yellow
- Write-Host "Password: `t`t$Password" -ForegroundColor Yellow
- $Answer = Read-Host "Is this information correct? (Y/N)"
- If($Answer.ToUpper() -eq "Y") {
- Write-Host "Creating" $UserName "now. Give this a few moments."
- #----------------------------------------------------------
- #
- # LOAD ASSEMBLIES AND MODULES - Create User in ActiveDirectory
- #----------------------------------------------------------
- Import-Module ActiveDirectory -ErrorAction SilentlyContinue
- New-ADUser `
- -Name ($LastName+""+$FirstName.Substring(0,1)) `
- -SamAccountName $UserName `
- -DisplayName ($FirstName+""+$LastName) `
- -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) `
- -ChangePasswordAtLogon $False -Enabled $True `
- Add-ADGroupMember "Users" "Users";
- Set-ADUser -Identity $UserName -PasswordNeverExpires $true #Don't forget the passwordneverexpires flag!
- Write-Host "Password never expires is now enabled"
- Start-Sleep -s 10 #Efficiency clock
- #----------------------------------------------------------
- #
- # Email the users information to management
- #----------------------------------------------------------
- Write-Host "Emailing this information to management" -ForegroundColor Green #If the info is correct, email the information.
- #Email the info to management
- $PSEmailServer = "EMAIL SERVER"
- $Port = 587
- 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."
- Start-Sleep -s 5 #Efficiency clock
- Write-Host "Done!" -ForegroundColor Red #Done sending the email
- if($Answer.ToUpper() -eq "N") { Write-Host "Restart the script!" -ForegroundColor RED #If the info is incorrect, have the user restart the script.
- Write-Host "This session will now be cleared in 10 seconds" -ForegroundColor Red
- Start-Sleep -s 10 #Efficiency clock
- cls
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement