29 Kasım 2016 Salı

Office 365 Account Creation and Bulk Email User Details with Powershell

If you have purchased office365 to be used in your organization, your first duty is to open accounts in office 365 after configuring related DNS settings in your DNS server.

To open these accounts in bulk you can use  office365 admin panel import pane. But without going to admin panel you can manage this tedious work using powershell.

The following powershell script will help you to

  • Create  Accounts
  • Assign Office365 License Pack you have
  • Export Created Account  details including assigned temproary passwords in NewAccountResults.csv file.  


Beforehand you should have  list of your staffs details in users.csv file.



#Pop-up will appear and wait you to enter administrator account credentials
$credential = get-credential

#Imports the installed Azure Active Directory module.
Import-Module MSOnline

#Establishes Online Services connection to Office 365 Management Layer.
Connect-MsolService -Credential $credential

#Create Users
Import-Csv .\users.csv | ForEach-Object {
#Generate Random Password
$office_365_password = ([char[]]([char]33..[char]95) + ([char[]]([char]97..[char]126)) + 0..9 | sort {Get-Random})[0..8] -join ''
#Create User
New-MsolUser -UserPrincipalName $_.UserPrincipalName -DisplayName $_.DisplayName -Password $office_365_password -UsageLocation $_.UsageLocation
}|Export-Csv -Path ".\NewAccountResults.csv"

#Assing License Pack to the unlicensed users
Get-MsolUser -All -UnlicensedUsersOnly  | Set-MsolUserLicense  -AddLicenses "yourdomain365:OFFICESUBSCRIPTION_PACKNAME"

Then you should send the temproary passwords created  to the personal email acounts of your staff by using the following powershell script.


#From : Sender Email Account 
$EmailFrom = "Name Surname <sender@domain.com>"
 
# Reporting: Report on Success and Failure (optional)
$EmailDeliveryNotificationOption = "onSuccess, onFailure"
 
# Server: Your Email SMTP server
$EmailSMTPserver = "mail.example.com"


# Users: csv file path, file includes  Name, PersonelEmail, Office365Account, Password
$SourcePath = ".\mail_merge_powerdshell.csv"
 
# Import csv file
$Users = Import-Csv  -Path $SourcePath
 
# ####################
# END Variables
# ####################
 
# Begin Loop: Do the following with each row of the file you imported, referencing columns by their header
foreach ($User in $Users) {
 
# To: User's email address
$EmailTo = $User.PersonelEmail
 
# Subject: Email subject (may merge variables)
$EmailSubject = "About Your Office365 Account " + $User.Name + "."
 
# Body: Email body, with HTML formatting
$EmailBody = "<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">"
$EmailBody += "<html xmlns=""http://www.w3.org/1999/xhtml""><head>"
$EmailBody += "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />"
$EmailBody += "<meta name=""viewport"" content=""width=device-width, initial-scale=1.0""/>"
$EmailBody += "<title>" + $EmailSubject + "</title>"
$EmailBody += "</head><body bgcolor=""#FFFFFF"" style=""font-family: sans-serif; color: #000000"">"
$EmailBody += "<p>Dear " + $User.Name + ":</p>"
$EmailBody += "<p>Our Organization has purchased Office365 subscription for 1 Year</p>"
$EmailBody += "<li>Your Office365 Account: <strong>" + $User.Office365Account + "</strong></li></ul></p>"
$EmailBody += "<li>Your Office365 Account Password: <strong>" + $User.Password + "</strong></li></ul></p>"
$EmailBody += "<li>You can Access Office Login to your office 365 using <a href='https://login.microsoftonline.com/'>This Link </a>"
$EmailBody += "</body></html>"
 
echo $EmailBody
echo $Users
# Merge: Conduct the email merge, sending emails (remove -WhatIf)
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHTML -SmtpServer $EmailSmtpServer -DeliveryNotificationOption $EmailDeliveryNotificationOption

İzleyiciler