Sunday, July 3, 2016

Send email from PowerShell and add an attachment -- send-MailMessage

In my role I am always running PowerShell routines daily or weekly to force accuracy in AD.  Whether its company naming convention or updating the manager field when new employees come aboard or old employees change roles I want to see what was updated to ensure my routine is still solid and not in need of any updates.  So I wrote this to be sure I can always find the information I need I email the change logs to myself at 6am so first thing in the morning I can verify my nightly process ran smooth.

#Variables
$log      = "location of the file you want to attach"
$smtp = "MailServer.YourDomain.com"
$to = "Recipents@YourDomain.com"
$from = "DNR_YourAddress@YourDomain.com"
$subject = "your subject"
$body = "Please see attached logs.

#”Your process that creates the file you want to email to a person or group goes on the next line”

#### Now send the email using \> Send-MailMessage

send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority normal -Attachments $log

No comments:

Post a Comment