{"id":134,"date":"2010-08-13T19:21:06","date_gmt":"2010-08-14T02:21:06","guid":{"rendered":"http:\/\/joshua.frederici.net\/blog\/?p=134"},"modified":"2010-08-13T19:21:06","modified_gmt":"2010-08-14T02:21:06","slug":"my-first-real-powershell-script","status":"publish","type":"post","link":"https:\/\/joshua.frederici.net\/blog\/my-first-real-powershell-script\/","title":{"rendered":"My First *REAL* PowerShell Script"},"content":{"rendered":"<p>So I&#8217;ve done a few one-liner&#8217;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. \u00a0It should be no coincidence that today&#8217;s workday was the conclusion of a week of PowerShell training from Microsoft.<\/p>\n<p>Anyway, the concept behind the script is simple. \u00a0I have a few network shares to which our deployment of Microsoft Configuration Manager sites perform their backups to. \u00a0I wanted a quick and easy way to be assured that those backups were completing properly. \u00a0This script does the following:<\/p>\n<ul>\n<li>Loop through each of the main SCCM backup folder shares (one at each major datacenter)<\/li>\n<li>Loop through each site backup folder<\/li>\n<li>Determine if folder was modified within the last 24 hours<\/li>\n<li>Determine if the backup task&#8217;s log file was modified within the last24 hours<\/li>\n<li>If above conditions are true, open the log file and ensure that it says that the task completed with no errors<\/li>\n<li>If above conditions aren&#8217;t true, note the directory of the failed backups.<\/li>\n<li>Report backup status to the console. \u00a0If backups failed, report SCCM site code.<\/li>\n<\/ul>\n<p>[codesyntax lang=&#8221;powershell&#8221;]<\/p>\n<pre>function Check-SCCMBackups\n{\n# $arrBackupFolders contains a list of locations that contain SCCM backups.\n$arrBackupFolders = \"\\\\server1\\share1\",\"\\\\server2\\share2\",\"\\\\server3\\share3\"\n$dateYesterday = (Get-Date).addDays(-1)\n# Initialize variables\n# variable to hold list of sites whose backups did not run\n$strSiteBackupNotRun = \"\"\n # variable to hold list of sites whose backups ran but failes\n$strSiteBackupFailed = \"\"\n$boolTasksDidRun = 1 \n# initialized to 1; set to 0 if any backup task did execute successfully\n$boolTasksRanSuccessfully = 1 \nWrite-Host \"Checking status of SCCM backup folders... \" -nonewline\n# Loop through each value of $arrBackupFolders....\nForEach ($strUNCPath in $arrBackupFolders)\n{\n# ... and get the directories it contains.\n$objBackupFolders = Get-ChildItem $strUNCPath | where {$_.PsIsContainer}\n# Loop through each discovered directory....\nForEach ($objDirectory in $objBackupFolders)\n{\n# ... and check it directory was modified within the last day.\nif ($objDirectory.LastWriteTime -ge $dateYesterday)\n{\n# Task executed (we know because folder write time was updated)\n# Open log file and determine if job completed successfully.\n$strLogFilePath = $strUNCPath + \"\\\" + $objDirectory.Name + \"\\\" + $objDirectory.Name + \"Backup\\smsbkup.log\"\n# Check smsbkup.log file modify time to ensure it was\n# modified within the last 24 hours.\n$strLogFile = Get-Item $strLogFilePath\n# File was NOT (!) modified in last 24 hours!\nIf (!$strLogFile.LastWriteTime -ge $dateYesterday)\n{\n# Since log file was not modified (something else must've written to the directory)....\n# Add Site Code (directory name) to the list of sites that did not run and set the flag...\n$strSiteBackupNotRun = $strSiteBackupNotRun + \" \" + $objDirectory.Name\n$boolTasksDidRun = 0\n# ...then stop processing this object and continue with the loop.\nContinue\n}\n# Line indicating success is four lines from the bottom of the file.\n$strLine = (Get-Content $strLogFilePath)[-4]\n# If the line doesnt match criteria, backup did not complete successfully.\nif ($strLine -notmatch \"Backup task completed successfully with zero errors\")\n{\n# Task ran but did not complete successfully\n# Add Site Code (directory name) to the list of failed backups....\n$strSiteBackupFailed = $strSiteBackupFailed + \" \" + $objDirectory.Name\n# ... and set the flag indicating that one or more tasks did not complete successfully.\n$boolTasksRanSuccessfully = 0\n}\n}\nelse\n{\n# Task did not run as the directory modified date is not within last day.\n# Add Site Code (directory name) to the list of sites that did not run and set the flag\n$strSiteBackupNotRun = $strSiteBackupNotRun + \" \" + $objDirectory.Name\n$boolTasksDidRun = 0\n}\n}\n}\nWrite-Host \"Complete!\"\n# Check status of boolean flags and present data.\nif ($boolTasksDidRun -eq 0 -OR $boolTasksRanSuccessfully -eq 0)\n{\n# One or more tasks did not execute or failed during execution.\nif ($boolTasksDidRun -eq 0)\n{\n# One or more tasks did nto execute; list the site codes (directory names).\nWrite-Host \"Backup tasks for the following sites did not run: $strSiteBackupNotRun\" -foregroundcolor red -backgroundcolor black\n}\nif ($boolTasksRanSuccessfully -eq 0)\n{\n# One or more tasks did not run successfully; list the site codes (directory names).\nWrite-Host \"Backup tasks for the following sites did not complete successfully: $strSiteBackupFailed\" -foregroundcolor red -backgroundcolor black\n}\n}\nelseif ($boolTasksDidRun -eq 1 -AND $boolTasksRanSuccessfully -eq 1)\n{\n# All tasks completed successfully\n# (Directory modify dates are within 24 hours and all log files show last task execution was successful.)\nWrite-Host \"All SCCM backup tasks executed and ran successfully.\" -foregroundcolor green\n}\nWrite-Host \"\"\n}<\/pre>\n<p>[\/codesyntax]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;ve done a few one-liner&#8217;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. \u00a0It should be no coincidence that today&#8217;s workday was the conclusion of a week of PowerShell training from Microsoft. Anyway, the concept [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-tech-configmgr","category-tech-powershell"],"_links":{"self":[{"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":0,"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joshua.frederici.net\/blog\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}