PowerShell script exchange
Post Reply
Rasmusen
Novice
Posts: 5
Liked: 1 time
Joined: Nov 06, 2019 2:47 pm
Contact:

Export backup history for selected vm´s

Post by Rasmusen »

Hi

Does anyone know how to collect backup history for a selected group of vm´s and export it to something like html....?
Its for audit of our backups.

I have tried the "job report" in the backup console but it displays all servers and i need to select specific vm´s

Best regards
Rasmus

Shestakov
Expert
Posts: 7328
Liked: 776 times
Joined: May 21, 2014 11:03 am
Full Name: Nikita Shestakov
Location: Prague
Contact:

Re: Export backup history for selected vm´s

Post by Shestakov »

Hi Rasmus,
Besides VBR and PS, you can leverage Veeam ONE reports. For this case VM Daily Protection Status should work. The report is available even in free(community) product edition.
Thanks!

Rasmusen
Novice
Posts: 5
Liked: 1 time
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen »

Hi Shestakov

Thanks :-)

We dont have Veeam ONE and are not planning to install this.
What im looking for is a simple PS script that can give me this output

regards
Rasmus

oleg.feoktistov
Veeam Software
Posts: 1006
Liked: 364 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Export backup history for selected vm´s

Post by oleg.feoktistov »

Hi Rasmus,

How do you intend to group VMs for a search? By specific network, resource pool, backup job? Or just get a report highlighting particular VM names?

Thanks,
Oleg

Rasmusen
Novice
Posts: 5
Liked: 1 time
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen »

Hi Oleg

Its for different audits and typical they provide 10 named vm´s and we should come up with an output where they can see backup history for the 10 vm´s a year back.

Regards
Rasmus

oleg.feoktistov
Veeam Software
Posts: 1006
Liked: 364 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Export backup history for selected vm´s

Post by oleg.feoktistov » 2 people like this post

Hi Rasmus,

This is what occurred to me:

Code: Select all


<#
    .SYNOPSIS
        Generates html report on Backups and Backup Copies.

    .SYNTAX
        Get-BackupReport -Path <string[]> [-Backup] [-BackupCopy]

    .PARAMETERS
        -path <string[]>

        -Backup <switch>

        -BackupCopy <switch>
#>
add-pssnapin -name VeeamPSSnapin
connect-vbrserver -server localhost
$styleHtml = @"
        <style>
        TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
        TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
        TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
        </style>
"@
function get-backupreport {
[cmdletbinding(defaultparametersetname = 'Path')]
param (
   [parameter(
        mandatory,
        parametersetname  = 'Path',
        valuefrompipeline,
        valuefrompipelinebypropertyname
    )]
    [validatenotnullorempty()]
    [string[]]$Path,

    [switch]$Backup,

    [switch]$BackupCopy
)


$sessions = get-vbrbackupsession
$sessionInfo = @()
$importPath = get-content -LiteralPath $Path
foreach ($import in $importPath) {
   foreach ($session in $sessions) {
        if ($Backup)
        {
        $backupInfo = get-vbrtasksession -session $session | where-object {$_.Name -eq $import -and $session.JobType -eq 'Backup'}
        }
        elseif ($BackupCopy)
        {
        $backupInfo = get-vbrtasksession -session $session | where-object {$_.Name -eq $import -and $session.JobType -eq 'BackupSync'}
        }
        else {
         $backupInfo = get-vbrtasksession -session $session | where-object {$_.Name -eq $import -and ($session.JobType -eq 'BackupSync' -or $session.JobType -eq 'Backup')}
        }
        if ($backupInfo) {
                $sessionStats = @{
                    VirtualMachine = $backupInfo.Name
                    Result = $backupInfo.Status
                    JobName = $session.JobName
                    JobType = $session.JobType
                    SessionStartTime = $session.CreationTime
                    SessionEndTime = $session.EndTime
                }
     $sessionInfo += $sessionStats | select-object @{n='VM Name';e={$_.VirtualMachine}}, @{n='Result';e={$_.Result}}, @{n='Job Name';e={$_.JobName}}, @{n='Job Type';e={$_.JobType}}, @{n='Start time';e={$_.SessionStartTime}}, @{n='End time';e={$_.SessionEndTime}}

 }
   }
   }
$exportDir = test-path -path 'C:\Temp'
$currentDate = (get-date).tostring('MM-dd-yyyy')
$exportTo = 'C:\Temp\BackupReport-' + $currentDate + '.html'
if (!$exportDir)
{
new-item -ItemType directory -Path 'C:\Temp'
$sessionInfo | convertto-html -Head $styleHtml | out-file -filepath $exportTo -Append
write-host 'The report was saved to C:\Temp\BackupReport.html'-ForegroundColor Green
}
elseif ($exportDir){
write-host 'The report was saved to C:\Temp\BackupReport.html'-ForegroundColor Green
$sessionInfo | convertto-html -Head $styleHtml | out-file -filepath $exportTo -Append
}

}


It is a function, which behaves like a cmdlet. It queries every backup/backup copy task session and pulls data for vms specified in a text file you pass to -path parameter.
So, if you import it to a powershell session with import-module from the directory it is saved in, further on you can use it in a session as get-backupreport cmdlet.
-path parameter takes a full path to the text file where vm names are contained.
-Backup and -BackupCopy are optional parameters. Specify one or another depending on your request.
If you won't specify it, the script will pull data for both Backups and Backup copies.

Example:

Code: Select all

import-module .\GetBackupReport.ps1
get-backupreport -path 'C:\Temp\Vms.txt' -Backup
I'm no powershell guru and the report is quite primitive, but feel free to play around with the parameters/variables to customize it.

Hope that helps.

Oleg

Rasmusen
Novice
Posts: 5
Liked: 1 time
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen »

Hi Oleg

Thank you i will look at it and report back the results.
Regards
Rasmus

chris.arceneaux
Veeam Software
Posts: 413
Liked: 222 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Kentucky, USA
Contact:

Re: Export backup history for selected vm´s

Post by chris.arceneaux » 1 person likes this post

@oleg.feoktistov Great job! You should put that on VeeamHub!

Rasmusen
Novice
Posts: 5
Liked: 1 time
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen » 1 person likes this post

Hi Oleg

The script works great and we have optimized it so that it suits our environment.
It takes around 15 minutes to run but that is no problem at all.
Thanks for helping :-)
Regards
Rasmus

tsightler
VP, Product Management
Posts: 5880
Liked: 2724 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Export backup history for selected vm´s

Post by tsightler » 2 people like this post

The original script looped through each VM in the file, and then each task sessions for every backup session in the VBR history, calling Get-VBRTaskSession mulitple times for each VM.

Since the script will need every backup session anyway, the version below moves the heavy lifting of getting all that data to the start of the script. It loads all the task session data into a hash table that uses the VM name as the key so that you can access every task session for a given VM very quickly. This completely eliminates the inner loop to go through each backup session for each VM. Now it simply loops through each VM imported from the file, references the hash table to grab all the task sessions and adds them to the session report in one go.

This should make the report run far faster, with bigger improvements for cases of exporting mulitple VMs in one run. In my lab the original code took >30 minutes to create a report for 7 VMs, and now runs in <30 seconds.

There are a few other minor tweaks as well, sort order of the output (easily changed), and using the task start/stop time vs the backup session start/stop time (I thought this made more sense but can revert easily if desired).

Code: Select all

<#
    .SYNOPSIS
        Generates html report on Backups and Backup Copies.

    .SYNTAX
        Get-BackupReport -Path <string[]> [-Backup] [-BackupCopy]

    .PARAMETERS
        -path <string[]>

        -Backup <switch>

        -BackupCopy <switch>
#>
add-pssnapin -name VeeamPSSnapin
connect-vbrserver -server localhost
$styleHtml = @"
        <style>
        TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
        TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
        TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
        </style>
"@
function get-backupreport {
[cmdletbinding(defaultparametersetname = 'Path')]
param (
   [parameter(
        mandatory,
        parametersetname  = 'Path',
        valuefrompipeline,
        valuefrompipelinebypropertyname
    )]
    [validatenotnullorempty()]
    [string[]]$Path,

    [switch]$Backup,

    [switch]$BackupCopy
)

if ($Backup) {
    $includeJobs = "^Backup$"
} elseif ($BackupCopy) {
    $includeJobs = "^BackupSync$"
} else {
    $includeJobs = "^Backup$|^BackupSync$"
}    
 
$sessions = Get-VBRBackupSession | ?{$_.JobType -match $includeJobs}
$taskSessions = $sessions.GetTaskSessions() | Group-Object -Property Name -AsHashTable

$sessionInfo = @()
$importPath = get-content -LiteralPath $Path
$sessionInfo = foreach ($import in $importPath) {
    $taskSessions.$import | Select-Object @{n='VM Name';e={$_.Name}}, @{n='Result';e={$_.Status}}, @{n='Job Name';e={$_.JobName}}, @{n='Job Type';e={$_.JobSess.JobType}}, @{n='Start time';e={$_.Progress.StartTimeLocal}}, @{n='End time';e={$_.Progress.StopTimeLocal}}
}

$sessionInfo = $sessionInfo | Sort-Object "VM Name", "Job Type", "Job Name", "Start Time"

$exportDir = Test-Path -path 'C:\Temp'
$currentDate = (Get-Date).tostring('MM-dd-yyyy')
$exportTo = 'C:\Temp\BackupReport-' + $currentDate + '.html'

if (!$exportDir) {
    New-Item -ItemType directory -Path 'C:\Temp'
    $sessionInfo | Convertto-Html -Head $styleHtml | Out-File -filepath $exportTo -Append
    Write-Host 'The report was saved to C:\Temp\BackupReport.html'-ForegroundColor Green
} elseif ($exportDir) {
    Write-Host 'The report was saved to C:\Temp\BackupReport.html'-ForegroundColor Green
    $sessionInfo | Convertto-Html -Head $styleHtml | Out-File -filepath $exportTo -Append
}

}

oleg.feoktistov
Veeam Software
Posts: 1006
Liked: 364 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Export backup history for selected vm´s

Post by oleg.feoktistov »

That's quite an optimization you made, Tom. Thank you, I really appreciate it!

kalaimani53
Influencer
Posts: 23
Liked: never
Joined: Jul 14, 2020 1:43 pm
Full Name: Kalaimani
Contact:

Re: Export backup history for selected vm´s

Post by kalaimani53 »

Dear All,

I'm Looking for the PowerShell script to get the one particular VM session history of the start time, end time, proceeded, transferred, Creating VM snapshot duration and Removing VM snapshot duration.

oleg.feoktistov
Veeam Software
Posts: 1006
Liked: 364 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Export backup history for selected vm´s

Post by oleg.feoktistov »

Hi,

I don't recall VMware having snapshot sessions information exposed through PowerCLI. As for other properties, they are all contained
inside session progress property:

Code: Select all

$session = Get-VBRBackupSession
$session.Progress
Thanks,
Oleg

Post Reply

Who is online

Users browsing this forum: lyya and 7 guests