It’s late on a Friday night. Like most people, you have somewhere else you would rather be. Instead, you are stuck at work because you completely forgot about some scheduled maintenance that has to be done that night.
You’re familiar enough with PowerShell to make some scripts to take care of the maintenance, but you’re worried about the off-chance that one of the scripts fails or you’re worried that something could go wrong.
Wouldn’t it be great if you could start your scripts and configure them to email you the results? You could still get out, enjoy your Friday night and rest assured that your scripts are doing their job without you losing yours!
(Disclaimer: We do not condone the shirking of any responsibilities that you may have…especially on Friday nights.)
There are a multiple ways to send an email with PowerShell. This is a native cmdlet option that is simple and easy to use. It uses the cmdlet Send-MailMessage.
List of available parameters
|–Attachments||string||The full path and filenames to be attached to the email.|
|–Bcc||string||Email addresses that you would like to bcc.|
|–Body||string||The body of the email message.|
|–BodyAsHtml||boolean||Indicates that the value of the Body parameter contains HTML.|
|–Cc||string||Addresses that you would like to cc.|
|–Credential||PSCredential||An account that has permission to send the email. The default is the current user.|
|-DeliveryNotificationOption||DeliveryNotificationOptions||Delivery notifications will be sent to the email address specified in the –From parameter. None – No notification (default) OnSuccess – Notify if the delivery is successful. OnFailure – Notify if the delivery is unsuccessful. Delay – Notify if the delivery is delayed. Never – Never notify.|
|–Encoding||Encoding||The encoding used for the body and subject.|
|–From||string||The address from which the mail is sent.|
|-Port||integer||Specifies an alternate port on the SMTP server. The default value is 25, which is the default SMTP port.|
|–Priority||MailPriority||The priority of the email message. Valid values: Low, Normal (default), High|
|–SmtpServer||string||The name of the SMTP server that sends the email message.|
|–Subject||string||The subject of the email message.|
|–To||string||The addresses you wish to send email to.|
|–UseSsl||boolean||Use the Secure Sockets Layer (SSL) protocol to establish a connection to the SMTP server to send email. SSL is not used by default.|
Here’s a full example of sending an email via Gmail’s SMTP servers. It will prompt you for a username and password thanks to (Get-Credential). The username needs to be and password needs to be , though you could always pipe in a PSCredential and get the same effect.
Best of luck to you in your emailing endeavors.
Did you know that PDQ Deploy has a PowerShell step you can use to deploy your scripts?
Kris was an employee at PDQ.