Wait & Notify via email When A Process Completes – Using PowerShell

A colleague of mine was copying a large file over the network using “RoboCopy“. As a good citizen, he throttled the copy using the “/IPG” switch of RoboCopy.

For information sake,

/ipg:n Specifies the inter-packet gap to free bandwidth on slow lines.

After starting the copy he informed me that it would complete in around 5 hours. That is when I got the bright idea to code a simple little routine in PowerShell to give him. It would check every 30 seconds to see if “robocopy” process is running and as soon as it completes, send me an email.


do {
    try
    {
        #Try to get the robocopy process (assuming only 1 is running).
        #  Throws an error if there is robocopy has completed
        $processing = get-process -Name robocopy -ErrorAction Stop
    }
    catch
    {
        if ($_.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.ProcessCommandException')
        {
            $processing = $null
        }
        else
        {
            throw
        }
    }

    sleep -seconds 30
    write-host "." -nonewline

} while ($processing)

Send-MailMessage `
    -Subject 'RoboCopy completed' `
    -Body 'The waited RoboCopy process completed' `
    -To 'me@mycompany.com' `
    -From 'sender@mycompany.com' `
    -Cc 'CoDBA@mycompany.com' `
    -SmtpServer 'OurSMTPServer.domain'

This is it. It is quite simple.

One of these days, I will convert this to a function which probably takes a script as input but for now, this quick and dirty method works!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s