Email Files with PowerShell

Email Files with PowerShell

PowerShell script to automatically email disaster recovery files from Tivoli Storage Manager to external email accounts for offsite backup.

I have a need when dealing with customers and their disaster recovery plans provided by Tivoli Storage Manager (TSM) to get these files offsite on a regular basis. Normally every day at about the same time. It’s a great idea to email them to yourself, however not such a great idea if the email is on the server you may need to recover.

I recommend in most cases that people get an external email account (Gmail, Live, Yahoo, etc.) and have the disaster recovery plans sent to them there. That way they are more likely to be able to retrieve them then if they were on the Exchange or Lotus Notes (Yes, people still use Notes for email) server that was in the datacenter that just imploded.

Configuration

You need to update a few things to make this work:

  • $SMTPServer – Make it your SMTP server
  • $DistributionList – I did it this way so you (or someone else) don’t need to edit the script when the recipients change
  • $SendingAddress – Who is this email going to be coming from?
  • $DataDirectory – What directory are the files kept in that need to be sent?
  • $RequiredFiles – The file names that need to be sent

In this instance the DR Plan itself is the last file to be created and has a different name everyday. I’m using the time difference to add it to the list of files that are needed.

PowerShell Script

# Author: Thomas Wimprine
# Creation Date: Dec 14, 2011
# Filename: SendDRPlan.ps1
# Description: Collect files needed for TSM Dr Recovery and email them to a distribution list

Function SendEmail {
    param (
        $FilesArray
    )
    $SMTPServer = "mail.domain.com"
    $DistributionList = "DRPlanRecipients@domain.com"
    $SendingAddress = "TSM@domain.com"

    # Create our mail message objects
    $ToAddress = New-Object System.Net.Mail.MailAddress $DistributionList
    $FromAddress = New-object System.Net.Mail.MailAddress $SendingAddress
    $Message = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress

    $Date = Get-Date
    $Date = $Date.ToShortDateString()
    $Message.Subject = "TSM DR Plan for $Date"
    $Message.Body = @("This is the daily DR plan as created by TSM with the required files to recover. Retain this message with attachments until it is no longer needed")

    # Add the attachments we need to the message
    foreach ($File in $FilesArray) {
        $Attachment = New-Object System.Net.Mail.Attachment($File,'text/plain')
        $Message.Attachments.Add($Attachment)
    }

    $Client = New-Object System.Net.Mail.SMTPClient $SMTPServer

    $Client.Send($Message)
}

Function GetLatestDRPlan {
    param ($Directory)
    foreach ($File in Get-ChildItem $Directory) {
        if ($NewestFile.CreationTime -lt $File.CreationTime) {
            $NewestFile = $File
        }
    }
    $NewestFile
}

$DataDirectory = "D:\DRPlanDir"
$RequiredFiles = "devconfig","volhist"
$RequiredFiles += GetLatestDRPlan($DataDirectory)

$AttachFiles = @()
foreach ($File in $RequiredFiles) {
    $AttachFiles += "$DataDirectory\$File"
}

SendEmail($AttachFiles)

This script automates the process of collecting and emailing critical disaster recovery files, ensuring they’re stored safely offsite in case of system failure.