Email Files with PowerShell

I have a need when dealing with customers and their disaster recovery plans provided by [Tivoli Storage Manager (TSM)](http://www-01.ibm.com/software/tivoli/products/storage-mgr/) 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](http://www.blogger.com/www.gmail.com), [Live](http://www.blogger.com/www.live.com), [Yahoo](http://www.blogger.com/www.yahoo.com), 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](http://www.blogger.com/www.microsoft.com/exchange) or [Lotus Notes](http://www-01.ibm.com/software/lotus/products/domino/) (Yes, people still use Notes for email) server that was in the datacenter that just imploded.
You need to update a few things and 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.
    # Author: Thomas Wimprine
    # Creation Date: Dec 14, 2011
    # Filename: SendDRPlan.ps1
    # Description: Collect files needed for TSM Dr Recovery and email them to a distibution list
     
    Function SendEmail {
        param (
            $FilesArray
        )
        $SMTPServer = "mail.domain.com"
        $DistributionList = "DRPlanRecipiants@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)
    ![](http://feeds.feedburner.com/~r/Ad/PowershellStuff/~4/6FlLey1oF5A)