Powershell Script for Email Automation

Posted on

Background to use powershell script:

Our company need to updates to related staff by sending them email when new files downloaded from our client’s website to 3 different folders in our file server. The files contain technical specification, orders and other work related material. The folder contain pdf and excel files. A staff will download the files to the folders and a program must send alert email to let merchandisers know what files has been added and their location, so the merchandisers can process the orders.

To achive this requirement I have tried some free software to check folder  content and send the email. But at that time I found that the programs i have tried not practical to be use for our case.

I decided to try to use powershell scripts to carry out this task.

Powershell installation:

If you are using windows 7 you just need to click start button, type powershell you will see 3 options:

  • Windows Powershell

It will open powershell prompt, you can type a script and press enter to execute

  • Windows Powershell ISE

Very useful if you want to write a long script, it’s better than just powershell prompt

  • Windows Powershell modules

More like powershell prompt but with better parameter help

start a powershell windows 7

Because we run the powershell in Windows 2003 server 64 bit, we have to install:
1. .NET 2.1 SP1 for 64 bit
2. WindowsServer2003-KB968930-x64-ENG.exe here

Below is the complete powershell script to send email and to check what new files for today to be followed up by merchandisers.

function sendMail{

Write-Host “Sending Email”

#SMTP server name
$smtpServer = “your SMTP server name”

#Creating a Mail object
$msg = new-object Net.Mail.MailMessage

#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

gci “D:\PTDHOME\2012\MERCHANDISING\PEERLESSWeb\CORD” | where { $_.DirectoryName -ne $NULL } | where { $_.LastWriteTime.Date -eq (Get-Date).Date } > D:\Peerless-txt\CORD-UPDATES.txt
gci “D:\PTDHOME\2012\MERCHANDISING\PEERLESSWeb\Poly-Rayon” | where { $_.DirectoryName -ne $NULL } | where { $_.LastWriteTime.Date -eq (Get-Date).Date } > D:\Peerless-txt\poly-rayon-UPDATES.txt
gci “D:\PTDHOME\2012\MERCHANDISING\PEERLESSWeb\Polyester” | where { $_.DirectoryName -ne $NULL } |  where { $_.LastWriteTime.Date -eq (Get-Date).Date } > D:\Peerless-txt\polyester-UPDATES.txt

$msg.To.Add(“email@address1,email@address2“)
if ((Get-Content “D:\Peerless-txt\CORD-UPDATES.txt”)  -ne $Null)
{
#Email structure
$msg.From = “email@address”
$msg.ReplyTo = “email@address”

$msg.subject = “Changed Files in Cord Product”
$msg.body = [string]::Join(“`r`n”,(Get-Content D:\Peerless-txt\CORD-UPDATES.txt))

#Sending email
$smtp.Send($msg)}

if ((Get-Content “D:\Peerless-txt\poly-rayon-UPDATES.txt”) -ne $Null)
{
$msg.From = “email@address”
$msg.ReplyTo = “email@address”
$msg.subject = “Changed Files in Poly Rayon Product”
$msg.body = [string]::Join(“`r`n”,(Get-Content D:\Peerless-txt\poly-rayon-UPDATES.txt))

#Sending email
$smtp.Send($msg)}

if ((Get-Content “D:\Peerless-txt\polyester-UPDATES.txt”) -ne $Null)
{
$msg.From = “email@address”
$msg.ReplyTo = “email@address”
$msg.subject = “Changed Files in Polyester Product”
$msg.body = [string]::Join(“`r`n”,(Get-Content D:\Peerless-txt\polyester-UPDATES.txt))

#Sending email
$smtp.Send($msg)}}

#Calling function
sendMail

This is the email result from above powershell commands:

powershell sending email with updated files status

I hope above samples of automation that powershell script can do will give you an idea on how to use this powerful script to do some automation works. I also found a great powershell script to check office 365 license type designation to each user in our organization here.

Facebook Comments