My First *REAL* PowerShell Script

Friday, 13. August 2010

So I’ve done a few one-liner’s in PowerShell to more efficiently collect certain information about a server, folder, etc, but today I wrote what I consider to be my first real PowerShell script.  It should be no coincidence that today’s workday was the conclusion of a week of PowerShell training from Microsoft.

Anyway, the concept behind the script is simple.  I have a few network shares to which our deployment of Microsoft Configuration Manager sites perform their backups to.  I wanted a quick and easy way to be assured that those backups were completing properly.  This script does the following:

  • Loop through each of the main SCCM backup folder shares (one at each major datacenter)
  • Loop through each site backup folder
  • Determine if folder was modified within the last 24 hours
  • Determine if the backup task’s log file was modified within the last24 hours
  • If above conditions are true, open the log file and ensure that it says that the task completed with no errors
  • If above conditions aren’t true, note the directory of the failed backups.
  • Report backup status to the console.  If backups failed, report SCCM site code.
function Check-SCCMBackups
{
# $arrBackupFolders contains a list of locations that contain SCCM backups.
$arrBackupFolders = "\\server1\share1","\\server2\share2","\\server3\share3"
$dateYesterday = (Get-Date).addDays(-1)
# Initialize variables
# variable to hold list of sites whose backups did not run
$strSiteBackupNotRun = ""
 # variable to hold list of sites whose backups ran but failes
$strSiteBackupFailed = ""
$boolTasksDidRun = 1 
# initialized to 1; set to 0 if any backup task did execute successfully
$boolTasksRanSuccessfully = 1 
Write-Host "Checking status of SCCM backup folders... " -nonewline
# Loop through each value of $arrBackupFolders....
ForEach ($strUNCPath in $arrBackupFolders)
{
# ... and get the directories it contains.
$objBackupFolders = Get-ChildItem $strUNCPath | where {$_.PsIsContainer}
# Loop through each discovered directory....
ForEach ($objDirectory in $objBackupFolders)
{
# ... and check it directory was modified within the last day.
if ($objDirectory.LastWriteTime -ge $dateYesterday)
{
# Task executed (we know because folder write time was updated)
# Open log file and determine if job completed successfully.
$strLogFilePath = $strUNCPath + "\" + $objDirectory.Name + "\" + $objDirectory.Name + "Backup\smsbkup.log"
# Check smsbkup.log file modify time to ensure it was
# modified within the last 24 hours.
$strLogFile = Get-Item $strLogFilePath
# File was NOT (!) modified in last 24 hours!
If (!$strLogFile.LastWriteTime -ge $dateYesterday)
{
# Since log file was not modified (something else must've written to the directory)....
# Add Site Code (directory name) to the list of sites that did not run and set the flag...
$strSiteBackupNotRun = $strSiteBackupNotRun + " " + $objDirectory.Name
$boolTasksDidRun = 0
# ...then stop processing this object and continue with the loop.
Continue
}
# Line indicating success is four lines from the bottom of the file.
$strLine = (Get-Content $strLogFilePath)[-4]
# If the line doesnt match criteria, backup did not complete successfully.
if ($strLine -notmatch "Backup task completed successfully with zero errors")
{
# Task ran but did not complete successfully
# Add Site Code (directory name) to the list of failed backups....
$strSiteBackupFailed = $strSiteBackupFailed + " " + $objDirectory.Name
# ... and set the flag indicating that one or more tasks did not complete successfully.
$boolTasksRanSuccessfully = 0
}
}
else
{
# Task did not run as the directory modified date is not within last day.
# Add Site Code (directory name) to the list of sites that did not run and set the flag
$strSiteBackupNotRun = $strSiteBackupNotRun + " " + $objDirectory.Name
$boolTasksDidRun = 0
}
}
}
Write-Host "Complete!"
# Check status of boolean flags and present data.
if ($boolTasksDidRun -eq 0 -OR $boolTasksRanSuccessfully -eq 0)
{
# One or more tasks did not execute or failed during execution.
if ($boolTasksDidRun -eq 0)
{
# One or more tasks did nto execute; list the site codes (directory names).
Write-Host "Backup tasks for the following sites did not run: $strSiteBackupNotRun" -foregroundcolor red -backgroundcolor black
}
if ($boolTasksRanSuccessfully -eq 0)
{
# One or more tasks did not run successfully; list the site codes (directory names).
Write-Host "Backup tasks for the following sites did not complete successfully: $strSiteBackupFailed" -foregroundcolor red -backgroundcolor black
}
}
elseif ($boolTasksDidRun -eq 1 -AND $boolTasksRanSuccessfully -eq 1)
{
# All tasks completed successfully
# (Directory modify dates are within 24 hours and all log files show last task execution was successful.)
Write-Host "All SCCM backup tasks executed and ran successfully." -foregroundcolor green
}
Write-Host ""
}

PowerShell Training This Week!

Monday, 9. August 2010

My employer has set up Microsoft training this week. We have scheduled to bring a trainer from Microsoft to our largest office who will provide a four day training starting tomorrow.

Because of this, I have been digging through some PowerShell resources I had previously found to get ahead of the curve for this training. I’m listing those resources here as a reminder to myself and in the hopes that somebody else may find them useful.