Powershell Send Mail Script

in scripts •  7 years ago 

Powershell Send Mail Script


Description


This script is designed to Execute a VBS script and then use Powershell to e-mail the VBS created report.
To use this script copy the contents of the script into notepad and save the file as FileName.ps1
Remember that in order to run scripts you may need to change the Powershell Execution Policy.


Usage


THIS SCRIPT REQUIRES MODIFICATION:

The following variables will need to be changed:

  • $From = "The e-mail address you wish the script to send mail as"
  • $To = "The e-mail address you wish to send the mail to"
  • $Subject = "The subject line of the e-mail"
  • $Body = "The text you wish to put in the body of the e-mail message"
  • $FileAttach = "The location and file name of the file you wish to attach to in the e-mail message"
  • $SMTPServer = "The name or IP of the e-mail server you which will relay the e-mail"


1.    Copy the script:

Copy the script and place the script in whatever directory that you will execute it from.


3.    Run the script:

Run the script using cscript to send the email notification.

cscript.exe "C:\Scripts\ScriptName.vbs"


Script


NOTICE:

This script can be copied as is. The top half containing fields such as from, to, subject, body, attachments, and the smtp server will need to be modified before execution.


$Date = get-date

$From = "[email protected]"
$To = "[email protected]"
$Subject = "Weekly Log Report - $Date"
$Body = "Please browse the included report for a list of Log files that have reached the set retention period and have been deleted"
$FileAttach = "C:\Scripts\LogReport.txt"
$SMTPServer = "exchange-server.somedomain.com"

$Attachment = new-object Net.Mail.Attachment($FileAttach)
$SMTP = new-object Net.Mail.SmtpClient($SMTPServer)
$MSG = new-object Net.Mail.MailMessage($From, $To, $Subject, $Body)
$MSG.attachments.add($Attachment)
$SMTP.send($msg)
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Write good