Skip to content

Check the last backup in Veeam for VMware – Powershell Script

by Patrick Durante on April 3rd, 2015

I had found the following script here .  Which would check if a VM had a backup run in a set amount of time.  I modified the script to send an email report.  Very useful to make sure that newly created VMs are getting backed up.


#Verify if Virtual Machines have been backed up. Put this file in a folder called c:\Scripts

asnp "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue
asnp "VeeamPSSnapIn" -ErrorAction SilentlyContinue

#-------------------------------------------------------
# Configuration

#For Vcenter server make sure you use the SAME EXACT NAME that Veeam uses in the managed servers vsphere tab.
$vcenter = "X.X.X.X"
$excludevms=@("xxxx","xxxx")
$DaysToCheck= 7

#Email Configuration
$toemail = "xxx@xxx.com"
$fromemail = "xx@xx.com"
$smtpserver = "x.x.x.x"
$subject = "Unprotected VM Report: No backup detected in $DaysToCheck days or no policy exists."

#-------------------------------------------------------
#-------------------------------------------------------
# Connect to vCenter
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | out-null
Connect-ViServer $vcenter | out-null

# Build hash table with excluded VMs
$excludedvms=@{}
foreach ($vm in $excludevms) {
$excludedvms.Add($vm, "Excluded")
}

# Get a list of all VMs from vCenter and add to hash table, assume Unprotected
$vms=@{}
foreach ($vm in (Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | ForEach-Object {$_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if (!$excludedvms.ContainsKey($vm.VMname)) {
$vms.Add($vm.VMname, "Unprotected")
}
}

# Find all backup job sessions that have ended in the last week
$vbrsessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -or $_.JobType -eq "Replica" -and $_.EndTime -ge (Get-Date).adddays(-$DaysToCheck)}

# Find all successfully backed up VMs in selected sessions (i.e. VMs not ending in failure) and update status to "Protected"
$backedupvms=@{}
foreach ($session in $vbrsessions) {
foreach ($vm in ($session.gettasksessions() | Where-Object {$_.Status -ne "Failed"} | ForEach-Object { $_ | Select-object @{Name="VMname";Expression={$_.Name}}})) {
if($vms.ContainsKey($vm.VMname)) {
$vms[$vm.VMname]="Protected"
}
}
}

#Remove my output file first from the last time it was run.
Remove-Item C:\scripts\bad.txt
Remove-Item C:\scripts\good.txt

# Output VMs in color coded format based on status.
foreach ($vm in $vms.Keys)
{
if ($vms[$vm] -eq "Protected") {
"$vm" | Add-Content C:\scripts\good.txt
} else {
"$vm"| Add-Content C:\scripts\bad.txt
}
}

#Echo the file and send the output, use the `n for new line.
$body = (Get-Content C:\scripts\bad.txt) -join "`n"

$email = @{
From = $fromemail
To = $toemail
Subject = $subject
SMTPServer = $smtpserver
Body = $body
}
send-mailmessage @email

From → Uncategorized

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

 

WP-SpamFree by Pole Position Marketing