Powershell

Automate Veeam Restores Using PowerShell

One of the powerful things that Veeam offers is the ability to use PowerShell to both backup and restore virtual machines.  This makes for great ways to automate tasks either backups or restores, or even other jobs via the PowerShell hooks into Veeam Backup & Replication.  In this post we will look at how to automate Veeam Restores Using PowerShell as it provides an extremely streamlined and much more efficient manner of restoring bulk virtual machines as opposed to the GUI interface.

vbrposh01

Automate Veeam Restores Using PowerShell

Ok, so let’s take a look at the code to see how we can automate restores using the PowerShell module for Veeam.  This is extremely handy especially if you are using Veeam to restore bulk virtual machines into a lab environment from production.

The first bit of code we will set some variables here to work with during the restore of the virtual machines.  We will use a text file to read the list of servers that we wish to restore in the lab environment.  In the following lines for vcentersuffixesxihost, populate information specific to your environment.  One variable I had issues with in the Veeam PowerShell commandlets was figuring out how to correctly populate the resourcepool information.  Using only the Find-VBRViEntity commandlet without scoping it always resulted in a $null value being returned for resourcepool.

A workaround for that issue was to run the command Find-VBRResourcePool -Server <your esxihost> and using the Reference returned in that command to scope our command below.  Populate your datastorefolder, and logfile with your information.

#Script to restore backups of VMs from Production into Lab Environment

$servers = get-content C:testservers.txt
$vcenter = "myvcenter.somewhere.com"
$suffix = '_suffix'
$esxihost = Get-VBRServer -Name "esxihost"
$resourcepool = Find-VBRViEntity -ResourcePools -Server $vcenter -Name "Resources" | where {$_.Reference -contains "resgroup-1111"}
$datastore = Find-VBRViDatastore -Name "mydatastore" -Server $esxihost
$folder = Find-VBRViFolder -Name "Labtest" -Server $vcenter
$logfile = "C:testlogfile.log"

Next, a function to do some logging for us (this function is used in other community sourced github PowerShell code:

Function My-Logger {
    param(
    [Parameter(Mandatory=$true)]
    [String]$message
    )

    $timeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"

    Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
    Write-Host -ForegroundColor Green " $message"
    $logMessage = "[$timeStamp] $message"
    $logMessage | Out-File -Append -LiteralPath $logfile
}

Next, is our for loop for actually doing the restoration work.  Note how easy with the Start-VBRRestoreVM command it is to define the folderdisktype, and skiptagsrestore options.  Especially with the disktype, if you are restoring several servers that have multiple disks and you want to select thin as the disk type, you have to in the GUI click every single one and choose thin.  There is not an option in the GUI to multiselect for the disk type.  Notice as well using the RunAsync parameter allows the jobs to all be kicked off by the Powershell commandlet.  If you don’t have this option included, it will kick off the first server, wait until it is fully restored, and then kick off the next server, etc.  This will take much longer than simply kicking them off asynchronously which allows the script to schedule the restores and complete.

foreach ($server in $servers) {

My-Logger "Getting Backups of server $server"
$backup = Get-VBRBackup -Name "$server"

My-Logger "Getting Last Restore Point of server $server"
$restorepoint = Get-VBRRestorePoint -Backup $backup | Select -Last 1

My-Logger "Starting the Restore Job for server $server"

Start-VBRRestoreVM -RunAsync -RestorePoint $restorepoint -Server $esxihost -ResourcePool $resourcepool -Datastore $datastore -VMName "$server$suffix" -Folder $folder -DiskType Thin -SkipTagsRestore

}

Now, lets look at the code all together.

#Script to restore backups of VMs from Production into Lab Environment

$servers = get-content C:testservers.txt
$vcenter = "myvcenter.somewhere.com"
$suffix = '_suffix'
$esxihost = Get-VBRServer -Name "esxihost"
$resourcepool = Find-VBRViEntity -ResourcePools -Server $vcenter -Name "Resources" | where {$_.Reference -contains "resgroup-1111"}
$datastore = Find-VBRViDatastore -Name "labloadstore" -Server $esxihost
$folder = Find-VBRViFolder -Name "Labtest" -Server $vcenter
$logfile = "C:windowsoptionslabrestorelabrestore.log"

Function My-Logger {
    param(
    [Parameter(Mandatory=$true)]
    [String]$message
    )

    $timeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"

    Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
    Write-Host -ForegroundColor Green " $message"
    $logMessage = "[$timeStamp] $message"
    $logMessage | Out-File -Append -LiteralPath $logfile
}


foreach ($server in $servers) {

My-Logger "Getting Backups of server $server"
$backup = Get-VBRBackup -Name "$server"

My-Logger "Getting Last Restore Point of server $server"
$restorepoint = Get-VBRRestorePoint -Backup $backup | Select -Last 1

My-Logger "Starting the Restore Job for server $server"

Start-VBRRestoreVM -RunAsync -RestorePoint $restorepoint -Server $esxihost -ResourcePool $resourcepool -Datastore $datastore -VMName "$server$suffix" -Folder $folder -DiskType Thin -SkipTagsRestore

}

Thoughts

The ability to Automate Veeam Restores Using PowerShell is a powerful way to quickly and much more efficiently kick off restores using the Veeam PowerShell commandlets.  Especially with mundane tasks that require “point and click” type work, using PowerShell is way better and saves a lot of heavy lifting with these kinds of tasks.  If you are using Veeam in your environment, be sure to take advantage of using PowerShell.

Subscribe to VirtualizationHowto via Email 🔔

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com and has over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, Brandon has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family.

Related Articles

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.