Wednesday, July 25, 2012

Powershell script to alert for missing snapshots

We had a problem with SMVI not taking backups and to make matters worse not alerting us to the fact that it was not taking backups , the software can only alert if the backup job fails or if it generates warnings which is fine.
But what happens when the job doesnt start , the SMVI service doesnt stop and nothing alerts you to the fact that no backups are taken... this was the second time this happened , the first time could be attributed to a random occurrence but the second time it happens ... well then your waiting to get caught with your pants around your ankles...
I made a short script , scheduled to run each evening , which would send a mail if there were no snapshots less than a day old.

#gets todays date , stores it in the format day-month-year in the variable $nowDate
$NowDate =get-date -uformat "%d-%m-%y"

#Loops through c:\snapshotsourcelist.txt assigning one line to the variable $snappath
For ($file = [System.IO.File]::OpenText("c:\snapshotsourcelist.txt");
     !($file.EndOfStream); $snappath = $file.readline()) 
{
#recursively scan through all subdirectories of $snappath
$Snapshotlist = get-childitem $snappath -recurse |
#we are only interested in files and not directories
where-object {$_.mode -notmatch "d"} |
#cant scan this for some reason so excluding it from the search criteria
Where-object {$_.name -notlike "*iegwydc01*"} |
#returns files which have a datestamp of today
where-object {$_.lastwritetime -gt [datetime]::parse($nowDate)}

#if the variable is empty then send a mail
if (!$Snapshotlist) {

write-host 'Snapshot Alert , No Snapshots for' +$snappath 'were taken since 00:00 last night'

        $SmtpClient = new-object system.net.mail.smtpClient
        $SmtpServer = "mailserver.domain.com"
        $SmtpClient.host =
        $SmtpServer

        $From = "ie-dlitalerts@domain.com"
        $To = "Darragh@domain.com"
        $Title = 'Snapshot Alert , No Snapshots for' +$snappath
        $Body = 'Snapshot Alert , No Snapshots for' +$snappath
        $SmtpClient.Send($from,$to,$title,$Body)  }

}

No comments:

Post a Comment