PowerCLI Script to check if VMs get backed up by Veeam B&R

PowerCLI Script to check if VMs get backed up by Veeam B&R

In this post I share a small PowerShell/PowerCLI script to check if VMs get backed up by Veeam B&R. If you do not run VeeamONE, it can be challenging to check if every VM that should be backed up, is really backed up. For checking this, this script should be helpful.

Basically this script looks for each VM if there exists a restore point for this VM. If not, it gets listed. 

Extra feature: 

  • Script connects to every vCenter that is registered in Veeam B&R server.
  • There is a blocklist (Blocklist.txt) included: when VMs should not be backed up, just place their name in the blocklist and they will not be shown in the list. So you can exclude VMs without editing the script.

Script to check VMs backed up by Veeam

# Load plugin and module
Add-PSSnapin VeeamPSSnapin
Import-Module VMware.VimAutomation.Core

# Configure PowerCLI for multiple vCenter connections
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false
# Ignore invalid certificates at vCenter connect
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Scope Session -Confirm:$false

# Connect to local B&R Server
Connect-VBRServer -Server localhost

# Read Credential-File
$WorkingDir = "C:\script\"
$CredObject = Import-Clixml -Path ($WorkingDir + "CredObject.xml")

# Connect to vCenter servers, added to B&R server
Get-VBRServer -Type VC | ForEach-Object {
    Connect-VIServer $_.name -Credential $CredObject -ErrorAction Continue
}

# Read VMs in blocklist
$Blocklist = Get-Content -path ($WorkingDir + "Blocklist.txt") -ErrorAction SilentlyContinue | Foreach {$_.TrimEnd()} 

# Read all VMs in vCenter(s)
$VMs = Get-VM | select Name, MemoryGB, PowerState, VMHost, Folder

# Query Veeam Restore Points
$result = @()
$VbrRestore = Get-VBRBackup | Where-Object {$_.jobtype -eq "Backup"} | ForEach-Object {$Jobname = $_.jobname; Write-Output $_;} | Get-VBRRestorePoint | Select-Object vmname, @{n="Jobname"; e={$Jobname}} |Group-Object vmname
$VbrRestore = $VbrRestore | ForEach-Object {$_.Group | Select-Object -first 1}

# Check, if VM on blocklist and a restore point exists
$VMs | ForEach-Object {
    if (($_.name -notin $Blocklist) -and ($_.name -notin $VbrRestore.vmname)) {
        $result += $_
    }
}
$result  | ft -AutoSize

# Close connections
Disconnect-VIServer * -Confirm:$false
Disconnect-VBRServer 

Notes

  • CredObject.xml is used for stored, encrypted credentials. See beneath how to create such a file.
  • Credential file and Blocklist.txt are expected to be in $WorkingDir.
  • The connection is done to the local B&R server.
  • Script can easily be adapted to check VM tags for exclusion/inclusion.
  • A Script to show last successful Veeam backup of VMs you can find here.

Credential file

To create en encrypted credential file, the following code can be used. Commands should be self-explained.

# Define your user and password
$UserName = ''
$UserPWD =  ''

$WorkingDir = "C:\script\"

# Convert password into secure-string
$SecStringPWD = ConvertTo-SecureString $UserPWD -AsPlainText -Force
# Create a PowerShell credential-object
$CredObject = New-Object System.Management.Automation.PSCredential ($UserName, $SecStringPWD )
# Save to file
$CredObject | Export-Clixml -Path ($WorkingDir + "CredObject.xml")

Leave a Reply

Your email address will not be published. Required fields are marked *