Hi, everyone! I have a PowerShell script that I use to retrieve some information about the daily backup sessions. It works well, but I was trying to transition it from the Get-VBR-Job command to the Get-VBRBackup command. Everything works but the newer script is very slow; it went from a few seconds with the old script to 5-10 minutes with the new. I was wondering if anyone could give me pointers on a more efficient way to get the session information or otherwise speed it up.
Thanks in advance!!
Robb
Original script
#backup Jobs
Foreach ($JobObject in Get-VBRJob){
#write-host "loop number ${$jobobject.indexof()}"
$session=$jobobject.FindLastSession()
if (!($JobObject.JobType -eq "BackupSync")) {
$JobOutput = New-Object -TypeName PSObject
if (((get-date).AddDays(-1) -lt $session.Progress.StopTimeLocal) -or ($session.Result -in "Failed","None")) {
$JobOutput | Add-Member -Name "Job Name" -MemberType Noteproperty -Value $JobObject.Name
$JobOutput | Add-Member -Name "Start Time" -MemberType Noteproperty -Value $session.Progress.StartTimeLocal
$JobOutput | Add-Member -Name "End Time" -MemberType Noteproperty -Value $session.Progress.StopTimeLocal
$JobOutput | Add-Member -Name "Duration" -MemberType Noteproperty -Value $session.Progress.Duration
if ($session.Progress.TransferedSize -lt 1KB) {
$xsize = $session.Progress.TransferedSize
$sz = "Bytes"
} elseif ($session.Progress.TransferedSize -lt 1MB) {
$xsize = $session.Progress.TransferedSize / 1KB
$sz = "KB"
} elseif ($session.Progress.TransferedSize -lt 1GB) {
$xsize = $session.Progress.TransferedSize / 1MB
$sz = "MB"
} elseif ($session.Progress.TransferedSize -lt 1TB) {
$xsize = $session.Progress.TransferedSize / 1GB
$sz = "GB"
} else {
$xsize = $session.Progress.TransferedSize / 1TB
$sz = "TB"
}
$xsize = "{0:n2}" -f $xsize
$JobOutput | Add-Member -Name "Data Transferred in GB" -MemberType Noteproperty -Value $xsize$sz
$JobOutput | Add-Member -Name "Result" -MemberType NoteProperty -Value $session.Result
if ((New-TimeSpan -Start $session.Progress.StartTimeLocal -end (Get-Date -f "MM/dd/yyyy HH:mm:ss tt")) -lt "24:00:00") {
$JobsOutput += $JobOutput
}}
}
}
$JobsOutput | Sort-Object -property 'Job Name' |Export-csv -Path $bfile -NoTypeInformation
New Script
# Backup Jobs
foreach ($jobobject in Get-VBRBackup) {
if ($JobObject.JobType -ne "SimpleBackupCopyPolicy") {
if ($jobobject.FindJob()) {
$sess = Get-VBRsession -job $jobobject.findjob() | sort -Property Creationtime -Descending | select -First 1
}
else
{
continue
}
$session = Get-VBRTaskSession -Session $sess
$JobOutput = New-Object -TypeName PSObject
if (((get-date).AddDays(-1) -lt $session.Progress.StopTimeLocal) -or ($session.Status -in "Failed","None")) {
$JobOutput | Add-Member -Name "Job Name" -MemberType Noteproperty -Value $JobObject.Name
$JobOutput | Add-Member -Name "Start Time" -MemberType Noteproperty -Value $session.Progress.StartTimeLocal
$JobOutput | Add-Member -Name "End Time" -MemberType Noteproperty -Value $session.Progress.StopTimeLocal
$JobOutput | Add-Member -Name "Duration" -MemberType Noteproperty -Value $session.Progress.Duration
switch ($session.Progress.TransferedSize) {
{$_ -lt 1KB} {$xsize = $session.Progress.TransferedSize
$sz = "Bytes"}
{$_-lt 1MB} {$xsize = $session.Progress.TransferedSize / 1KB
$sz = "KB"}
{$_ -lt 1GB} {$xsize = $session.Progress.TransferedSize / 1MB
$sz = "MB"}
{$_ -lt 1TB} {$xsize = $session.Progress.TransferedSize / 1GB
$sz = "GB"}
Default {$xsize = $session.Progress.TransferedSize / 1TB
$sz = "TB"}
}
$xsize = "{0:n2}" -f $xsize
$JobOutput | Add-Member -Name "Data Transferred in GB" -MemberType Noteproperty -Value $xsize$sz
$JobOutput | Add-Member -Name "Result" -MemberType NoteProperty -Value $session.Status
if ((New-TimeSpan -Start $session.Progress.StartTimeLocal -end (Get-Date -f "MM/dd/yyyy HH:mm:ss tt")) -lt "24:00:00") {
$JobsOutput += $JobOutput
}
}
}
}
$JobsOutput | Sort-Object -property 'Job Name' |Export-csv -Path $bfile -NoTypeInformation