Powershell

Automate Veeam Restores Using PowerShell

Today we look at how to Automate Veeam Restores Using PowerShell which provides a much more powerful and efficient way to restore to lab environments, etc

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ย vcenter,ย suffix,ย esxihost, 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ย datastore,ย folder, 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ย folder,ย disktype, 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 a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He 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. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

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.