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 ""
}

One Response to “My First *REAL* PowerShell Script”



  1. Rahul Says:

    Hi,

    Can you make some changes in the script.

    1. takes the server name and look for the backup file automatically on the server.
    2. for remote DB it should check the backup status.
    if the backup is partial success in this case also it should report the failure with details.

Leave a Reply