21 Desember, 2016

PowerShell to bulk email your users - MS Powershell script combine .csv


This morning suddenly my boss ask me to create mail blast to all AD (Active Directory) users due to our mail system migration from exchange on premise to office 365 mail services. Through mail blast users can know their new password (that set manually by IT Infrastructure team)

This article from Chris save my day: http://www.cgoosen.com/2015/04/user-powershell-to-bulk-email-your-users/

Below is my result:




 CSV file:


 Here is the powershell script:

# Function to create report email
function SendNotification{
 $Msg = New-Object Net.Mail.MailMessage
 $Smtp = New-Object Net.Mail.SmtpClient($ExchangeServer)
 $Msg.From = $FromAddress
 $Msg.To.Add($ToAddress)
 $Msg.Subject = "User mail Office 365 password"
 $Msg.Body = $EmailBody
 $Msg.IsBodyHTML = $true
 $Smtp.Send($Msg)
}

# Define local Exchange server info for message relay. Ensure that any servers running this script have permission to relay.
$ExchangeServer = "yourexchange.domain.com"
$FromAddress = "IT Team "

# Import user list and information from .CSV file
$Users = Import-Csv ITUserList.csv

# Send notification to each user in the list
Foreach ($User in $Users) {
 $ToAddress = $User.Email
 $Name = $User.FirstName
 $NewEmail = $User.EmailO365
 $Password = $User.PasswordO365
 $EmailBody = @"
 
 
 
 
 Dear $Name,


 As you know our email system will migrate to Microsoft Office 365. This email contains your new email password to login.


 Your account is:


 Office 365 mail account: $NewEmail

 Office 365 mail password: $Password


 Please reset your password as soon as possible after you login into office 365 for the first time.


 If you require any assistance during the migration please contact the IT Helpdesk at it.helpdesk@domain.com or by calling Ph +6221-XXXXXX | Ext 712 / 741


 Regards,


 IT Team

 
 
"@
 Write-Host "Sending notification to $Name ($ToAddress)" -ForegroundColor Yellow
 SendNotification
}
Rinardi Adinegara Barlim Ms SharePoint System Developer

God's partner that full of love in his life. Enjoy his life by distributed bless to another people that he meet. Now work as Microsoft SharePoint Developer :)

Tidak ada komentar :

Blog Stat