Friday, June 15, 2012

Sending Email from a PowerShell Script

As I move more batch jobs and scheduled tasks over to PowerShell, I've found that I often want to send some sort of status message when a script completes. The .NET Framework, which PowerShell uses, includes the Net.Mail.SMTPClient object that you can use to send email messages.

First you need to create a new instance of the object in a variable:
$mail= New-Object Net.Mail.SmtpClient("IPorDNSofSmtpServer")

Then, you can send the message:
$mail.Send("FromAddress","Recipient1,Recipent2","Subject","Body")

In most cases, you would use variables to define the addresses, subject, and body of the message.


Some potentially useful documentation:

No comments:

Post a Comment