In the first part of the article, we learned what PowerShell is, when we should consider using it, and how to use basic PowerShell cmdlets. This part contains practical examples of Hyper-V backup and recovery-related operations using PowerShell scripts with Veeam.
What is Veeam Backup PowerShell snap-in?
Since v5, Veeam Backup & Replication has had a built-in PowerShell snap-in (a package of cmdlets) that can make your life as a backup administrator much easier. Veeam PowerShell snap-in provides a set of its own available commands, which correlate with actions that the administrator can perform in the Veeam Backup & Replication GUI. There’s no limit to the list of available operations; it all depends only on the administrator’s creativity!
Thinking about killing routine or willing to have a few one-line cmdlets handy, so you can quickly check backup information right from the console? Either way, this Veeam snap-in is worth knowing.
NOTE: All actions performed with PowerShell have the same impact as actions performed with the Veeam GUI. Once you make a change, the change is applied to the Veeam Backup database and you may not be able to undo it.
Getting started with Veeam PowerShell snap-in
Veeam requires PowerShell version 2.0 and newer, which should be safe for Windows Server 2008 R2 and later releases. For older OS versions, check this manual.
Open the Veeam console and navigate to the main menu. Since Veeam PowerShell snap-in isn’t installed by default, you might not be able to see an available PowerShell option as shown in the figure below.
Figure 1. Veeam Backup & Replication, the main menu
To add the PowerShell snap-in, follow these steps:
- Close the Veeam Backup & Replication console
- Put the Veeam installation disk (.iso) back to the system
- Modify the installation with the guidelines from this KB article
Now you should be able to find PowerShell in the main menu and launch it.
Figure 2. Veeam Backup & Replication PowerShell Toolkit
This is a special PowerShell toolkit window with the loaded Veeam snap-in. The advantage of using it is that you won’t have to add Veeam snap-in every time you’re executing the command, and some predefined cmdlets like Get-VBRCommand are available. The toolkit is especially helpful when you’d like to check something very quickly — one-line query — rather than writing scripts.
Since I’m about to write some scripts, I’ll still be using Windows PowerShell ISE editor. Don’t forget to use Add-PSSnapin VeeamPSSnapin or asnp VeeamPSSnapin (asnp is an alias for Add-PSSnapin) command to add Veeam snap-in and click the Refresh button so you can see that the PowerShell cmdlets available for Veeam operations.
Figure 3. Windows PowerShell ISE, module VeeamPSSnapin
NOTE: VBR stands for Veeam Backup & Replication, VSB stands for Veeam SureBackup and HV stands for Hyper-V.
Veeam PowerShell script examples
Scrolling through the list of existing Veeam cmdlets should give you an idea of possible actions to make. Ready to experiment? Let’s do it together.
It might be quite handy to check a list of VMs on any Hyper-V host added to Veeam console so we can do any operation with a desired VM. Filter cmdlets by find and locate the cmdlet you need — “Find-VBRHvEntity”. Once you click on it, you see additional parameters available. Fill them in, execute the command and see how it looks properly:
Find-VBRHvEntity -Name * -Server localhost
Obviously, this is too much information to show, which is why you can apply a condition to this output: place a pipe and add Select-Object Name, Type in order to list only names of VMs and hosts.
Figure 4. A list of Hyper-V VMs and hosts added to Veeam console
Now, we can use this information to perform a quick backup of a Hyper-V VM, aka VeeamZIP. For this purpose, we should define a variable of Hyper-V object, which is done by using the name of VM. Then we complement this with the Start-VBRZip cmdlet, as seen below:
Script to perform VeeamZIP backup of a selected Hyper-V VM:
# To add Veeam PowerShell snap-in
asnp VeeamPSSnapin
# Define Virtual Machines to backup with VeeamZIP
$HVObject = Find-VBRHvEntity -Server localhost -Name 'winxp'
# Start VeeamZIP job
Start-VBRZip -BackupRepository "Default Backup Repository" -Entity $HVObject -Compression 5
Put it together, execute (F5) and see how Veeam is taking the VeeamZIP backup of a Hyper-V VM onto the default backup repository (C:\backup).
This is how it looks in PowerShell editor:
Figure 5. VeeamZIP backup of selected Hyper-V VM
You can open the Veeam console and find the same operation in the GUI:
Figure 6. VeeamZIP backup of selected Hyper-V VM, GUI
Note: The Start-VBRZip cmdlet is supported starting from Veeam Backup Free Edition 8.0 Update 2 and is not supported on previous versions.
Now that we have a backup of a VM, we can restore it back to the server. Find a restore script below. It’s better to read through it from the bottom so you can understand the logic behind all variables.
Script to perform a Hyper-V VM files restore from the last backup point:
# Script to perform a Hyper-V VM files restore from a last backup point
#add Veeam PowerShell Snap-in
asnp VeeamPSSnapin
# define Veeam Repository we’re doing the restore from
Get-VBRBackupRepository -name "Default Backup Repository"
# define the last backup point of the VM (VMname), included to the job “Winxp backup”
$lastpoint = Get-VBRBackup -Name "Winxp Backup" | Get-VBRRestorePoint -Name winxp | Sort-Object $_.creationtime -Descending | Select -First 1
# get the list of the files from the needed backup point
$Filestorestore = Get-VBRFilesInRestorePoint ($lastpoint)
# define the server where to perform restore to
$server = Get-VBRserver | where {$_.name -eq "This Server"}
# define the path for restore on the selected server
$path = "D:\restore"
# execute restore
Start-VBRRestoreVMFiles -RestorePoint $lastpoint -Server $server -Files $Filestorestore -path $path
Execute it and get Hyper-V VM files to be restored from the latest backup point to local server.
Figure 7. Hyper-V VM files restore, in action
And the same process by Veeam console interface:
Figure 8. Hyper-V VM files restore, GUI
Even though it was rather simple examples of how you can utilize PowerShell for Hyper-V backup and restore tasks, I hope it was helpful and that it was a good starting point for your PowerShell journey. Now that you’ve had some practice, feel free to practice more and check out the following resources:
- Veeam Forums dedicated to PowerShell scripts
- Veeam Backup Free Edition: Now with PowerShell
- Learn PowerShell. From Basics to a Coding Star
- The PowerShell Paradigm (live webinar)
Have a good time with PowerShell, and don’t forget to share your experience with us.