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
$emailSender
$emailRecipients
$smtpServer
$log_file