Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Beginning of functions
- Function Format-Email {
- Param ([string]$WCEmailContent,[string]$UserPrincipalName,[string]$UserPassword)
- return $WCEmailContent + "User Name = $UserPrincipalName
- Password = $UserPassword
- Thank You
- IT Department"
- }
- Function Send-Email {
- Param ($Email, $Credential,[string]$WCEmailContent)
- $From = "karim.buzdar@gmail.com"
- $subject = "Domain Account Details"
- $Body = $WCEmailContent
- $SMTPServer = "smtp.gmail.com"
- $SMTPPort = "587"
- Send-MailMessage -from $From -to $Email -Subject $subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -Credential $Credential -UseSsl
- }
- ###End of Functions
- ##### Beginning of main programme
- $Credential = (get-Credential) #Getting email credentials to be used for From field in Email message
- $UsersFilePath = "C:\Users\Administrator.YOURDOMAIN\Desktop\WC\users.csv" # Files of users information (First name, Last name, Sam account name, Email address)
- $WCEmailFile = "C:\Users\Administrator.YOURDOMAIN\Desktop\WC\WCEmail.docx" # File containing welcome email message
- import-csv -path $UsersFilePath | foreach {
- $Name = ($_.GivenName + " " + $_.LastName)
- $UserPrincipalName = ($_.SamAccountName + ”@yourdomain.com”)
- $UserPassword = Get-Random -maximum 20000 -Minimum 100
- $UserPassword = "@" + "user" + $UserPassword.ToString()
- #Create user in Students OU
- new-aduser -name $Name -enabled $true -GivenName $_.GivenName –surname $_.lastName -accountpassword (convertto-securestring $UserPassword -asplaintext -force) -changepasswordatlogon $true -samaccountname $_.SamAccountName –userprincipalname $UserPrincipalName -EmailAddress $_.EmailAddress -Path 'OU=Students,DC=yourdomain,DC=com' -ErrorAction Stop
- $WCEmailContent = Format-Email -WCEmailContent (Get-Content $WCEmailFile) -UserPrinciPalName $UserPrincipalName -UserPassword $UserPassword
- Send-Email -Email $_.EmailAddress -Credential $Credential -WCEmailContent $WCEmailContent
- Write-Host "User Created: $Name"
- }
- ### End of main program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement