PowerShell script exchange
Post Reply
cloudy
Novice
Posts: 9
Liked: 13 times
Joined: Mar 16, 2016 8:42 pm
Contact:

VBR Powershell Reporting: Restore jobs in last X days

Post by cloudy »

Hello Everyone,
I wrote this script to keep track of veeam restores in Veeam 9. Now that the console has been decentralized I find it hard to keep track of who has done what, easily, without digging through piles of logs. This powershell script will send an html email report for all veeam restore jobs run within the defined time period, default is 7 days. I'm running this as a weekly scheduled task, but one could certainly run it every day or any other time period. If no restore jobs have taken place in the time period defined an email report will not be generated. This script will also log the collected information to a local log file on the server where the script was executed for historical purposes. This script has only been tested with VBR restore jobs and with powershell v4. Script has not been tested with BEM restores.

Report contents:
Resource Restored - VM name
Start Time
End Time
Restore Type - Guest File Restore (Windows), Guest File Restore (Multi-OS), Full VM Restore, etc
Restore Type Detail - Similar information as restore type: LinuxFileLevelRestore, RestoreVM, FileLevelRestore, etc
Restore Status - Success/Fail
Restore Reason - Text typed into the box during restore job setup
Console host and User which ran the job

HTML Email Report:
The report is sent via email in html format with CSS styling. The data is returned in an easy to read formatted table. To view a sample html report comment out line 67, starting with "Send-MailMessage" and uncomment line 61, "$htmlbody | Out-File -FilePath ".\html.htm"" and run the script. html file will be placed in the same location where the script ran from.

Local log file sample output: (if run more than once a day, log file will append to the days log file)

Code: Select all

--- Veeam Backup Report - Restore Jobs Run In Last 7 Days 20160314_0620 ----


JobName       : FLR_[<server_name_here>]
CreationTime  : 3/9/2016 10:07:40
EndTime       : 3/9/2016 10:10:37
JobType       : FileLevelRestore
JobTypeString : Guest File Restore (Windows)
Options       : <console_host>\<username>
Description   : Testing File Restore
Result        : Success

Powershell Code:

Code: Select all

Asnp VeeamPSSnapin 
$date = (get-date -format MM/dd/yy)
$restoreJobs = Get-VBRRestoreSession | where {((Get-Date) - ($_.CreationTime)).totaldays -le "7"} |Sort-Object Creationtime | Select JobName, CreationTime, EndTime, JobType, JobTypeString, Options, Description, Result
$restoreJobData= @()
foreach ($restoreJob in $restoreJobs) {
    $separator = " ";
    $separator1 = '`"+" "';
    $option = [System.StringSplitOptions]::RemoveEmptyEntries;
    $jobOptions = $restoreJob.Options | out-string;
    $username = $jobOptions.Split($separator,$option)|Select-String "InitiatorName="|out-string;
    $username = $username.Substring($username.indexof('"')+1);
    $username = $username -replace '"';
    $reason = $jobOptions -Split '" '|Select-String "Reason="|out-string;
    $reason = $reason.Substring($reason.indexof('"')+1);
    $restoreJob.Options=$username;
    $restoreJob.Description=$reason; 
    $restoreJobDataObject = $restoreJob;
    $restoreJobData += $restoreJobDataObject;
}
if ($restoreJobData.count -eq 0) {
    exit
}

$head=@"
<style>
@charset "UTF-8";

#content {
width:75%;
border:0px solid #005a99;
padding-left:50px;
}
 
h2 {
color:#0888d8;
}

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    text-align: left;
    padding: 8px;
}

tr:nth-child(odd){background-color: #f2f2f2}

th {
    background-color: #4997C7;
    color: white;
}

</style>
"@

$htmlbody = $restoreJobData|Sort-Object Creationtime | Select @{Name="Resource Name";Expression={$_.JobName}},@{Name="Start Time";Expression={$_.CreationTime}},@{Name="End Time";Expression={$_.EndTime}},@{Name="Job Type";Expression={$_.JobTypeString}},@{Name="Job Type Detail";Expression={$_.JobType}},@{Name="Restore Status";Expression={$_.Result}},@{Name="Restore Reason";Expression={$_.Description}},@{Name="Run By";Expression={$_.Options}} | ConvertTo-HTML -Head $head -PreContent "<div id=""content"" class=""transparent""><H2>Veeam Backup Report - Restore Jobs In Last 7 days - $($date)</H2>" -PostContent "</div>"| out-string

#use as sample output file
#$htmlbody | Out-File -FilePath ".\html.htm"

$emailSender = "<sender_email_address>"
[string[]]$emailRecipients = "<email_recipients>" #comma delimited: "user1@example.com","user2@example.com"
$emailSubject = "Veeam Backup Report - Restore Jobs In Last 7 days - $($date)"
$smtpServer = "<smtp_server>"
Send-MailMessage -From $emailSender -Subject "$($emailSubject)" -To $emailRecipients -Body $htmlbody -BodyAsHtml -SmtpServer $smtpServer

$logcurrent_time = (get-date -format yyyyMMdd\_HHmm)
$logdate = (get-date -format yyyyMMdd)
$log_file = "<log_file_path>\veeam_report_restore_jobs_last_7_days_$($logdate).log"
Add-Content "--- Veeam Backup Report - Restore Jobs Run In Last 7 Days $($logcurrent_time) ----" -path $log_file -encoding unicode
$restoreJobData|Sort-Object Creationtime | Select JobName, CreationTime, EndTime, JobType, JobTypeString, Options, Description, Result| Out-File -FilePath $log_file -Append
To use change the following variable values:
$emailSender
$emailRecipients
$smtpServer
$log_file
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: VBR Powershell Reporting: Restore jobs in last X days

Post by tsightler »

cloudy wrote:Now that the console has been decentralized I find it hard to keep track of who has done what, easily, without digging through piles of logs.
Not that I don't think this script isn't great (it's awesome), but I'm curious about this statement. Can't you just go to history and select the "Restore" node? That will list all restore actions, file, application, VM, etc., and then there are even separate nodes in the tree for each category if you want to see only all VM restores or all application restores. I'm not clear how decentralizing the GUI made that more difficult.

But great job on the script, I can still see how a weekly report might be nice, especially in cases where there are mulitple B&R consoles. There is a similar report available in Veeam ONE for those that have implemented it.
cloudy
Novice
Posts: 9
Liked: 13 times
Joined: Mar 16, 2016 8:42 pm
Contact:

Re: VBR Powershell Reporting: Restore jobs in last X days

Post by cloudy »

*facepalm* Thanks for that bit of info. Yes, you are correct! I've never seen that before. The history option on my console is a tiny little icon in the bottom corner of the menu panel that i've glossed over a few million times. I learned something new today, thanks!
favlass
Lurker
Posts: 1
Liked: never
Joined: Jun 02, 2017 9:44 am
Full Name: Fabrice Vlassembrouck
Contact:

Re: VBR Powershell Reporting: Restore jobs in last X days

Post by favlass »

Thanks for this script.

Is it possible to get the details of the restored elements ?

Thanks for your reply.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests