Veeam v7 – Backing up a single VM within a job

Find the latest version here.

One of the best things about Powershell and the community is the ability to easily find what others have done and improve upon it or tweak it for your own needs. This was the case with a script I found in the Veeam forums a few months back to backup a single VM using Powershell. It worked great (with a minor issue described below) but unfortunately was broken after upgrading to v7.  I was able to identify the issues causing it to fail but was not able to rectify them all. Back to the forums I went and with a little help from the folks at Veeam we were back in business.

There is one ‘gotcha’ to this script. It relies on the fact that VMs are added to the job individually and not via some form of container such as datastore, resource pool, etc. The reason for this lies within the magic of the script itself. Basically what the script does is exclude all objects in the job except the VM you want to back up. If we exclude a container that holds the VM we want to back up then the exclusion overrides and nothing gets backed up. So as long as you create your jobs using individual VMs the script should work like a charm.

# Load Veeam snapin
Add-PsSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
Write-Host "VM must be explicitly defined in the job"
Write-Host "VM can not be inherited by a folder, datastore or other object or it will get excluded as well and job will be empty."
Write-Host ""
Write-Host "Always confirm job completion via Veeam BR Console"
Write-Host ""
# Check Veeam Version (Must be v7)
If ((Get-PSSnapin VeeamPSSnapin).Version.Major -ne 7) {
Write-Host "You must be running VBR v7 to run this script…Exiting"
# User Input
$jobName = Read-Host "Enter Job Name"
$vmName = Read-Host "Enter VM Name"
# Find the job that has our VM
$job = Get-VBRJob | ?{$_.Name -eq $jobName}
# Get all objects in job apart from our target VM
$execObjs = $job.GetObjectsInJob() | ?{$_.Name -ne $vmName}
# Exclude the objects from the job(*Note: this isn't removing the objects
# from the job)
Remove-VBRJobObject -Objects $execObjs
# Start the job only backing up the target VM
Start-VBRJob -Job $job
# Find the exclude job objects
$incObjs = $job.GetObjectsInJob() | ?{$_.Type -eq "Exclude"}
# Delete the exclude objects and re-add to job
foreach ($obj in $incObjs) {
$Excitem = $obj.GetObject().GetItem()
Add-VBRViJobObject -job $Job -Entities $Excitem
$obj.Delete() | Out-Null

5 thoughts on “Veeam v7 – Backing up a single VM within a job

  1. Phil

    Great script :-), any chance you’ll post an updated version for Veeam 8? The script makes an error when it tries to put the excluded vm’s back in the job…

      1. Phil

        Quick reply, thanks… 🙂 unfortunately I need it to backup a newly added vm (full backup), and this feature only supports vm’s that have already been backed up…

  2. Pingback: Veeam v8 – Backing up a single VM with PowerShell | Shawn Masterson's Blog

Leave a Reply

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