Friday, 11 April 2014

Email functionality with Power Shell

There are multiple methods to send an emails using power shell script, below are the few methods i used to follow.......

Use below script to send test email from Power Shell...


$To = "SPAdmin@org.com" #change your email id to send an email
$fromemail = "SPReports@org.com" #Add your from email address
$server = "smtp.org.com"  #change your SMTP address
$TodayDate = Get-Date

send-mailmessage -from $fromemail -to $users -subject "Test Reports" -body "Test Report for $TodayDate" -priority Normal -smtpServer $server


Email with HTML Body.......


$To = "SPAdmin@org.com" #change your email id to send an email
$fromemail = "SPReports@org.com" #Add your from email address
$server = "smtp.org.com"  #change your SMTP address
$TodayDate = Get-Date
#creating message body with  list of services in server...

$MessageBody = Get-Service | Select-Object Status, Name, DisplayName | ConvertTo-HTML  -body "<H2>Service Information</H2>" |  Out-String

#sending email.....

send-mailmessage -from $fromemail -to $users -subject "Test Reports" -BodyAsHTML -body $MessageBody -priority Normal -smtpServer $server













No comments:

Post a Comment