Powershell script to report daily backup sizes

PowerShell script exchange

Powershell script to report daily backup sizes

Veeam Logoby pt2k11 » Fri May 04, 2012 2:26 pm

We are building a script to report on the daily backup jobs using powershell.

We have the following so far but are unable to get the sizes of the actual backup files using the cmdlets. Can anyone advise how we can get this informaiton into the html report?

Code: Select all
#load Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
#Style
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:orange}
TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:lightblue}
tr.special {background: #000080;} <tr class="special"></tr>
</style>
"@

$Report = @()
$Jobs = Get-VBRJob | ?{$_.Name -match "server01"}

foreach ($job in $Jobs) {
$jobName = $job.Name
$table = New-Object system.Data.DataTable "$table01"

#region Setup table columns
$col1 = New-Object system.Data.DataColumn Index,([int])
$col2 = New-Object system.Data.DataColumn JobName,([string])
$col3 = New-Object system.Data.DataColumn VMList,([String])
$col4 = New-Object system.Data.DataColumn StartTime,([DateTime])
$col5 = New-Object system.Data.DataColumn StopTime,([DateTime])
$col6 = New-Object system.Data.DataColumn FileName,([string])
$col7 = New-Object system.Data.DataColumn CreationTime,([DateTime])
$col8 = New-Object system.Data.DataColumn AvgSpeedMB,([int])
$col9 = New-Object system.Data.DataColumn Duration,([TimeSpan])
$col10 = New-Object system.Data.DataColumn Result,([String])


$table.columns.add($col1)
$table.columns.add($col2)
$table.columns.add($col3)
$table.columns.add($col4)
$table.columns.add($col5)
$table.columns.add($col6)
$table.columns.add($col7)
$table.columns.add($col8)
$table.columns.add($col9)
$table.columns.add($col10)
#endregion

#Grab all Backup Sessions on the server where their .JobId property is the same as the Get-VBRJob objects .Id property
$session = Get-VBRBackupSession | ?{$_.JobId -eq $job.Id} | %{
$row = $table.NewRow()
$row.JobName = $_.JobName
$row.StartTime = $_.CreationTime
$row.StopTime = $_.EndTime
#Work out average speed in MB and round this to 0 decimal places, just like the Veeam GUI does.
$row.AvgSpeedMB = [Math]::Round($_.Progress.AvgSpeed/1024/1024,0)
#Duration is a Timespan value, so I am formatting in here using 3 properties - HH,MM,SS
$row.Duration = '{0:00}:{1:00}:{2:00}' -f $_.Progress.Duration.Hours, $_.Progress.Duration.Minutes, $_.Progress.Duration.Seconds

if ($_.Result -eq "Failed") {
#This is highlight is going to later be searched and replaced with HTML code to highlight failed jobs in RED 
$row.Result = "#HIGHLIGHTRED"+$_.Result+"HIGHLIGHTRED#"
} else {
#Don't highlight if the backup session didn't fail.
$row.Result = $_.Result
}
#Add this calculated row to the $table.Rows
$table.Rows.Add($row)
}

$interestingsess = $table | Sort StartTime -descending | select -first 7
$pkc = 1
$interestingsess | foreach {
#for every object in $interestingsess (which has now been sorted by StartTime) assign the current value of $pkc to the .Index property. 1,2,3,4,5,6 etc...
$_.Index = $pkc
#Increment $pkc, so the next foreach loop assigns a higher value to the next .Index property on the next row.
$pkc+=1
}


#Now we are grabbing all the backup objects (same as viewing Backups in the Veeam B&R GUI Console
$backup = Get-VBRBackup | ?{$_.JobId -eq $job.Id}

$points = $backup.GetStorages() | sort CreationTime -descending | Select -First 7 #Find and assign the Veeam Backup files for each job we are going through and sort them in descending order. Select the specified amount.
#Increment variable is set to 1 to start off
$ic = 1
ForEach ($point in $points) {
#Match the $ic (Increment variable) up with the Index number we kept earlier, and assign $table to $rows where they are the same. This happens for each object in $points
$rows = $table | ?{$_.Index -eq $ic}




#inner ForEach loop to assign the value of the backup point's filename and VMs to the row's .FileName property as well as the creation time.
ForEach ($row in $rows) {
$Backups = Get-VBRBackup | ?{$_.JobId -eq $job.Id}
$vms =
foreach ($Backup in $Backups) {
$Backup.GetObjects() | Select Name
}
$vms1 = $vms | out-string
$vms1 = $vms1.replace(" " , "")
$vms1 = $vms1.replace("Name" , "")
$vms1 = $vms1.replace("----" , "")
$vms1 = $vms1.replace("`r" , "")
$vms1 = $vms1.replace("`n" , ";")
$vms1 = $vms1.replace(";;;" , "")
$vms1 = $vms1.replace("; ; ;" , "")
$vms1 = $vms1.replace(" ;" , "; ")
($row.FileName = $point.FileName) -and ($row.CreationTime = $point.CreationTime) -and ($row.VMList = $vms1)


#Increment the $ic variable ( +1 )
$ic+=1
}


}
#Tally up the current results into our $Report Array (add them)
$Report += $interestingsess
}


#Now we select those values of interest to us and convert the lot into HTML, assigning the styling we defined at the beginning of this script too.
$Report = $Report | Select Index, JobName, VMList, StartTime, StopTime, FileName, CreationTime, AvgSpeedMB, Duration, Result| ConvertTo-HTML -head $style

#Interesting bit - replace the highlighted parts with HTML code to flag up Failed jobs.
$Report = $Report -replace "#HIGHLIGHTRED","<font color='red'><B>"
$Report = $Report -replace "HIGHLIGHTRED#","</font></B>"
#Finally, save the report to a file on your drive.
$Report | Set-Content C:\VBRpowershell\Veeam-Backup-Report.htm

pt2k11
Lurker
 
Posts: 1
Liked: never
Joined: Wed May 25, 2011 1:50 pm
Full Name: Platform Team

Re: Powershell script to report daily backup sizes

Veeam Logoby ThomasMc » Fri May 04, 2012 7:08 pm

Quick Q, v5 or 6?
ThomasMc
Expert
 
Posts: 293
Liked: 17 times
Joined: Wed Apr 13, 2011 12:45 pm
Full Name: Thomas McConnell

Re: Powershell script to report daily backup sizes

Veeam Logoby Platform1 » Thu May 24, 2012 9:41 am

We're using version 6.
Platform1
Influencer
 
Posts: 13
Liked: never
Joined: Tue Jul 19, 2011 12:14 pm
Full Name: Scott Cooney

Re: Powershell script to report daily backup sizes

Veeam Logoby pizzim13 » Fri May 25, 2012 5:22 pm

Code: Select all
($backup.GetStorages() | Select-Object -ExpandProperty stats | ForEach-Object {$_.backupsize} | Measure-Object -Sum).sum
will return total backup size in bytes.
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: Powershell script to report daily backup sizes

Veeam Logoby 1-0-1 » Thu Jun 14, 2012 1:48 pm

And with v5?
1-0-1
Enthusiast
 
Posts: 54
Liked: never
Joined: Tue Nov 30, 2010 1:38 pm
Full Name: Bernd

Re: Powershell script to report daily backup sizes

Veeam Logoby pizzim13 » Fri Jun 15, 2012 1:45 pm

Same code works
pizzim13
Enthusiast
 
Posts: 86
Liked: 6 times
Joined: Thu Apr 21, 2011 7:37 pm

Re: Powershell script to report daily backup sizes

Veeam Logoby JohnPA » Wed Jun 27, 2012 7:44 am

Did p2k11 get this script to work and would he/she care to share it?
JohnPA
Novice
 
Posts: 5
Liked: never
Joined: Thu Oct 13, 2011 8:42 am


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 5 guests