PowerShell script exchange
Post Reply
Rasmusen
Novice
Posts: 4
Liked: never
Joined: Nov 06, 2019 2:47 pm
Contact:

Export backup history for selected vm´s

Post by Rasmusen » Nov 06, 2019 2:54 pm

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
Veeam Software
Posts: 7009
Liked: 717 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 » Nov 06, 2019 4:08 pm

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: 4
Liked: never
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen » Nov 07, 2019 9:45 am

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: 33
Liked: 12 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 » Nov 07, 2019 11:34 am

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: 4
Liked: never
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen » Nov 07, 2019 1:05 pm

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: 33
Liked: 12 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 » Nov 13, 2019 5:09 pm 1 person likes 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: 4
Liked: never
Joined: Nov 06, 2019 2:47 pm
Contact:

Re: Export backup history for selected vm´s

Post by Rasmusen » Nov 21, 2019 9:29 am

Hi Oleg

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

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests