Guest User

Backup Exec backup report via PowerShell

a guest
Jan 21st, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     REQUIREMENTS
  3.     Symantec Backup Exec (TM) 2014
  4.     .Net Framework 3.5
  5. #>
  6.  
  7. # normally the first import line should be enough
  8. # but due to the user context that the script may run under when executed by labtech
  9. # we need the second import that has an explicit path
  10. # so I'm keeping both lines just in case
  11. import-module bemcli
  12. import-module "c:\program files\symantec\backup exec\modules\bemcli\bemcli"
  13.  
  14.  
  15. # START SERVICE CHECK
  16. # many of the cmdlets will not run properly if some of the services are not running
  17. # so here we enumerate all the services, and if their state isn't running we restart them
  18. $services = get-beservice
  19. foreach($service in $services){
  20.     if($service.status -ne "Running"){
  21.         restart-beservice $service.name
  22.     }
  23. }
  24. # END SERVICE CHECK
  25.  
  26.  
  27. # START GLOBALS
  28. # get all jobs that exist, who's schedule is NOT null, AND are NOT Unscheduled, AND are type Backup OR Duplicate,  AND task types are Full OR Incremental OR Duplicate, and sort the results by name
  29. $jobs = get-bejob | ?{($_.schedule -ne $null) -and ($_.schedule.tostring() -ne "Unscheduled") -and ($_.jobtype -eq "Backup" -or $_.jobtype -eq "Duplicate") -and ($_.tasktype -eq "Full" -or $_.tasktype -eq "Incremental" -or $_.tasktype -eq "Duplicate" -or $_.tasktype -eq "CustomBackup")} | sort name
  30. # number of days to show in the horizontal table, calendar style
  31. $daysHistory = 30
  32. # number of dats to show in the vertical, detailed job history table
  33. $daysDetail = 10
  34.  
  35. # colors for html table elements
  36. $cSuccess = "palegreen"
  37. $cWarning = "lemonchiffon"
  38. $cError = "lightsalmon"
  39. $cTableHeader = "blue"
  40. $cColumnHeader = "lavender"
  41. $cRowEven = "gainsboro"
  42.  
  43. # css style
  44. $style = "<style>body{font-size:10px;font-family:verdana,sans-serif;color:black;font-weight:normal;}" + `
  45. "table{width:100%;border-width:1px;cellpadding:0;cellspacing:0;border-style:solid;border-color:black;border-collapse:collapse}" + `
  46. "th{background:$cColumnHeader;white-space:nowrap;font-size:11px;border-width:1px;padding:5px;border-style:solid;border-color:black;}" + `
  47. "tr{font-size:11px;border-width:1px;padding:10px;border-style:solid;border-color:black;}" + `
  48. "tr:nth-child(even){background:$cRowEven}" + `
  49. "td{font-size:10px;border-width:1px;padding:4px;border-style:solid;border-color:black;}" + `
  50. "td:first-child{white-space:nowrap;width:1px}</style>"
  51. # END GLOBALS
  52.  
  53.  
  54. # START GENERATE HISTORY DATA
  55. # generate the beginning on the table and the column group
  56. # extra columns for name, status, and next scheduled date
  57. $history = "<table>`n<colgroup><col/><col/><col/>"
  58. for ($d=0; $d -lt $daysHistory; $d++){
  59.     $history += "</col>"
  60. }
  61. $history += "<colgroup/>`n"
  62.  
  63. # generate the headers
  64. $history += "<tr><th>Name</th><th>Status</th><th>Scheduled Next</th>"
  65. for ($d=0; $d -lt $daysHistory; $d++){
  66.     $history += "<th>"+(get-date).adddays(0-$d-1).ToShortDateString()+"</th>"
  67. }
  68. $history += "</tr>`n"
  69.  
  70. # generate the data in each row
  71. foreach ($job in $jobs){
  72.     # open the row for this job
  73.     $history += "<tr>"
  74.     # put the job name, status, and next scheduled in the first three columns / "<td>"++"</td>"
  75.     if($job.Status -eq "Active" -or $job.Status -eq "Dispatched" -or $job.Status -eq "Ready"){
  76.         # color the job, status, and date if it's active/dispatched/ready
  77.         $history += "<td style=`"font-weight: bold; background-color:$cSuccess`">"+$job.Name+"</td><td nowrap style=`"font-weight: bold; background-color:$cSuccess`">"+$job.Status+"</td><td nowrap style=`"font-weight: bold; background-color:$cSuccess`">Started: "+$job.NextStartDate+"</td>"
  78.     }elseif($job.Status -eq "OnHold"){
  79.         # color the job, status, and date if it's on hold
  80.         $history += "<td style=`"background-color:$cWarning`">"+$job.Name+"</td><td nowrap style=`"background-color:$cWarning`">"+$job.Status+"</td><td nowrap>"+$job.NextStartDate+"</td>"
  81.     }else{
  82.         $history += "<td >"+$job.Name+"</td><td nowrap>"+$job.Status+"</td><td nowrap>"+$job.NextStartDate+"</td>"
  83.     }
  84.     # put the history of the job from each day in to the rest of the columns
  85.     for ($d=0; $d -lt $daysHistory; $d++){
  86.         # get the job history for particular day, it's possible it ran multiple times
  87.         $jobHistory = $job | Get-BEJobHistory -fromstarttime (get-date).adddays(0-$d-1).Date -ToStartTime (get-date).adddays(0-$d).Date
  88.         # if more than one job history per day, process it like an array of objects
  89.         if($jobHistory.length -gt 1){
  90.             # shade cell background based on status
  91.             # here we check of any object of the collection contains the desired status, this means there is precedence
  92.             # for example, if "Succeeded" is matched, we will use that color regardless of what the jobstatus of other objects in the collection is
  93.             # this works out desireable as we usually just care about a single success per day
  94.             if(($jobHistory | ?{$_.JobStatus -contains "Succeeded"}) -ne $NULL){
  95.                 $history += "<td style=`"background-color:$cSuccess`">"
  96.             }elseif((($jobHistory | ?{$_.JobStatus -contains "Canceled"}) -ne $NULL) -or (($jobHistory | ?{$_.JobStatus -contains "Error"}) -ne $NULL)){
  97.                 $history += "<td style=`"background-color:$cError`">"
  98.             }elseif((($jobHistory | ?{$_.JobStatus -contains "SucceededWithExceptions"}) -ne $NULL) -or (($jobHistory | ?{$_.JobStatus -contains "Missed"}) -ne $NULL)){
  99.                 $history += "<td style=`"background-color:$cWarning`">"
  100.                
  101.             }else{
  102.                 $history += "<td>"
  103.             }
  104.             # since this is a collection of objects, we need to parse them and aggregate the statuses for display purposes
  105.             foreach($item in $jobHistory){
  106.                 $history += $item.JobStatus.toString()+" "
  107.             }
  108.         # if just one job history per day, process it as a single object
  109.         }else{
  110.             # shade cell background based on status
  111.             if($jobHistory.JobStatus -contains "Succeeded"){
  112.                 $history += "<td style=`"background-color:$cSuccess`">"
  113.             }elseif($jobHistory.JobStatus -contains "Canceled" -or $jobHistory.JobStatus -contains "Error"){
  114.                 $history += "<td style=`"background-color:$cError`">"
  115.             }elseif($jobHistory.JobStatus -contains "SucceededWithExceptions" -or $jobHistory.JobStatus -contains "Missed"){
  116.                 $history += "<td style=`"background-color:$cWarning`">"
  117.                
  118.             }else{
  119.                 $history += "<td>"
  120.             }
  121.             $history += $jobHistory.JobStatus
  122.         }
  123.         $history += "</td>"
  124.     }
  125.     #close the row for this job
  126.     $history += "</tr>`n"
  127. }
  128. $history += "<table/>`n"
  129. # STOP GENERATE HISTORY DATA
  130.  
  131. # START GENERATE DETAIL DATA
  132. $detailErrorCanceled = get-bejob | Get-BEJobHistory -FromStartTime (get-date).adddays(0-$daysDetail-1).Date -ToStartTime (get-date).Date | ?{$_.jobstatus -eq "Error" -or $_.jobstatus -eq "Canceled"} | sort StartTime -descending | select Name,JobType,JobStatus,@{name='Size (GB)';expression={"{0:N2}" -f ($_.TotalDataSizebytes/1073741824)}},StartTime,ElapsedTime,ErrorMessage | Convertto-html -fragment
  133. $detailSucceeded = get-bejob -jobtype backup | Get-BEJobHistory -FromStartTime (get-date).adddays(0-$daysDetail-1).Date -ToStartTime (get-date).Date -jobstatus Succeeded | sort StartTime -descending | select Name,JobType,JobStatus,@{name='Size (GB)';expression={"{0:N2}" -f ($_.TotalDataSizebytes/1073741824)}},StartTime,ElapsedTime | Convertto-html -fragment
  134. $detailSucceededWE = get-bejob -jobtype backup | Get-BEJobHistory -FromStartTime (get-date).adddays(0-$daysDetail-1).Date -ToStartTime (get-date).Date -jobstatus SucceededWithExceptions | sort StartTime -descending | select Name,JobType,JobStatus,@{name='Size (GB)';expression={"{0:N2}" -f ($_.TotalDataSizebytes/1073741824)}},StartTime,ElapsedTime | Convertto-html -fragment
  135.  
  136. $detail = "<table><tr><th style=`"text-align: center; font-weight: bold; background-color: $cError`">JOBS WITH ERRORS/CANCELED</th></tr></table>`n" + `
  137.             $detailErrorCanceled + "`n" + `
  138.             "<table><tr><th style=`"text-align: center; font-weight: bold; background-color: $cWarning`">SUCCESSES WITH EXCEPTIONS</th></tr></table>`n" + `
  139.             $detailSucceededWE + "`n" + `
  140.             "`n<table><tr><th style=`"text-align: center; font-weight: bold; background-color: $cSuccess`">SUCCESSES</th></tr></table>`n" + `
  141.             $detailSucceeded + "`n"
  142. # STOP GENERATE DETAIL DATA
  143.  
  144.  
  145. # START GENERATE STORAGE DATA
  146. $storage = "<table>`n<colgroup><col/><col/><col/><col/><col/><col/></colgroup>`n" + `
  147.         "<tr><th>Disk Name</th><th>Type</th><th>Path</th><th>Is Online</th><th>Status</th><th>Used/Total (GB) - Used %</th></tr>`n"
  148. $disks = Get-BEDiskStorageDevice | sort name
  149.  
  150. foreach($disk in $disks){
  151.     # if capacity % is above threshold, print the row out with alternate colors
  152.     # first check is for the highest usage, or red alert at 95%
  153.     if(($disk.UsedCapacityBytes/$disk.TotalBackupStorageBytes*100) -gt 95){
  154.         $storage += "<tr style=`"background-color: $cError`"><td>"+$disk.Name+"</td>" + `
  155.             "<td nowrap>"+($disk.Servers)[0].ConnectionType+"</td>" + `
  156.             "<td nowrap>"+($disk.Servers)[0].Path+"</td>" + `
  157.             "<td nowrap>"+($disk.Servers)[0].IsOnline+"</td>" + `
  158.             "<td nowrap>"+$disk.CapacityStatus+"</td>" + `
  159.             "<td nowrap>"+"{0:N2}" -f ($disk.UsedCapacityBytes/1073741824)+"/{0:N2}" -f ($disk.TotalBackupStorageBytes/1073741824)+" - {0:N2}" -f($disk.UsedCapacityBytes/$disk.TotalBackupStorageBytes*100)+"%</td></tr>`n"
  160.     # second check is for the warning usage level, or yellow alert at 85%
  161.     }elseif(($disk.UsedCapacityBytes/$disk.TotalBackupStorageBytes*100) -gt 85){
  162.         $storage += "<tr style=`"background-color: $cWarning`"><td>"+$disk.Name+"</td>" + `
  163.             "<td nowrap>"+($disk.Servers)[0].ConnectionType+"</td>" + `
  164.             "<td nowrap>"+($disk.Servers)[0].Path+"</td>" + `
  165.             "<td nowrap>"+($disk.Servers)[0].IsOnline+"</td>" + `
  166.             "<td nowrap>"+$disk.CapacityStatus+"</td>" + `
  167.             "<td nowrap>"+"{0:N2}" -f ($disk.UsedCapacityBytes/1073741824)+"/{0:N2}" -f ($disk.TotalBackupStorageBytes/1073741824)+" - {0:N2}" -f($disk.UsedCapacityBytes/$disk.TotalBackupStorageBytes*100)+"%</td></tr>`n"
  168.     # otherwise we default to normal rows
  169.     }else{
  170.         $storage += "<tr><td>"+$disk.Name+"</td>" + `
  171.             "<td nowrap>"+($disk.Servers)[0].ConnectionType+"</td>" + `
  172.             "<td nowrap>"+($disk.Servers)[0].Path+"</td>" + `
  173.             "<td nowrap>"+($disk.Servers)[0].IsOnline+"</td>" + `
  174.             "<td nowrap>"+$disk.CapacityStatus+"</td>" + `
  175.             "<td nowrap>"+"{0:N2}" -f ($disk.UsedCapacityBytes/1073741824)+"/{0:N2}" -f ($disk.TotalBackupStorageBytes/1073741824)+" - {0:N2}" -f($disk.UsedCapacityBytes/$disk.TotalBackupStorageBytes*100)+"%</td></tr>`n"
  176.     }
  177. }
  178. $storage += "<table>`n"
  179. # STOP GENERATE STORAGE DATA
  180.  
  181.  
  182. # START COMPILE OUTPUT
  183. $output = "<html><head>$style</head>"
  184. $output += "<body>" + `
  185.             "<table><tr><th style=`"font-size:12px;text-align: center; font-weight: bold; color: white; background-color: $cTableHeader`">JOB HISTORY (last $daysHistory days)</th></tr></table>`n" + `
  186.             $history + "`n" + `
  187.             "<table><tr><th style=`"font-size:12px;text-align: center; font-weight: bold; color: white; background-color: $cTableHeader`">STORAGE DETAILS</th></tr></table>`n" + `
  188.             $storage + "`n" + `
  189.             "<table><tr><th style=`"font-size:12px;text-align: center; font-weight: bold; color: white; background-color: $cTableHeader`">DETAILED JOB BREAKDOWN (last $daysDetail days)</th></tr></table>`n" + `
  190.             $detail + "`n" + `
  191.             "</body>"
  192.  
  193. $output | out-file ($args[0])
  194. # STOP COMPILE OUTPUT
Advertisement
Add Comment
Please, Sign In to add comment