Restore VMs via PowerShell

Restore VMs via PowerShell

Today I had the challenge to restore once a week multiple VMs that are used for VPN Client Testing.

I would like to share my script with you 😉

# Restore VMs for VPN Client Test

#Load PS Modules
Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VeeamPSSnapin
#Timer start
$StartTime = Get-Date
#Define Logging Vars
$verboseLogFile = 'c:\temp\myReports\older-reports\Restore-VM-Log_' + (Get-Date -Format yyyy-MM-dd) + '.txt'
$LastverboseLogFile = 'c:\temp\myReports\Restore-VM_latest.txt'
Function My-Logger {
    $timeStamp = Get-Date -Format "yyyy-MM-dd_HH:mm:ss"
    Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
    Write-Host -ForegroundColor Green " $message"
    $logMessage = "[$timeStamp] $message"
    $logMessage | Out-File -Append -LiteralPath $verboseLogFile
#Start Processing ...
My-Logger "Start VPN Client VMs restore"
##Connect Veeam & vCenter Server
My-Logger "Connect vCenter Server"
#Connect vCenter Server
Connect-VIServer -server my-vCenterServer.mydomain.local
My-Logger "Connect Veeam Server"
#Connect Veeam Server
Connect-VBRServer -Server my-VeeamServer.mydomain.local
## Define Vars
$BackupName = "MyBackup-01" #Enter Backupname here
My-Logger "Backup Name: $BackupName"
$vmfolder = "MyVMFolder" #VMware Folder
My-Logger "VMware Folder: $vmfolder"
## Fix Vars
$vmarray = '@' #Create Array
$vmarray = Get-VBRBackup -Name ($BackupName) | Get-VBRRestorePoint -Name * | select VmName #select VMs
My-Logger "VMs to restore ..."
$vmarray *>> $verboseLogFile
# Loop through all of the array Items to perform Restore
foreach ($vm in $vmarray) {
   # Get VMName from array
   $Name = $vm.VmName
   My-Logger "Restoreing VM: $Name"
   #Get a list of available restore points, select the last restore point and save it into a variable
   $RestorePoint = Get-VBRBackup -Name ($BackupName) | Get-VBRRestorePoint -Name ($Name) | select -Last (1)
   #Perform Restore of the VM:
   Start-VBRRestoreVM -RestorePoint ($RestorePoint) -Reason "Scheduled VPN Client VMs restore" -ToOriginalLocation -StoragePolicyAction Default
My-Logger "Restore Complete!"
My-Logger "Poweron VMs ..."
#Power On VMs
$poweronvm = Get-Folder $vmfolder | Get-VM | Where-Object {$_.PowerState -eq 'PoweredOff'}
$poweronvm *>> $verboseLogFile
Start-VM -VM $poweronvm -Confirm:$false -RunAsync
#Log powererd on VMs
My-Logger "Show Poweredon VMs ..."
Get-Folder $vmfolder | Get-VM *>> $verboseLogFile
#Calculate Time
$EndTime = Get-Date
$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
My-Logger "StartTime: $StartTime"
My-Logger "  EndTime: $EndTime"
My-Logger " Duration: $duration Minutes"
My-Logger "Complete!"
Copy-Item $verboseLogFile $LastverboseLogFile
Disconnect-VIServer * -Confirm:$false

The log would look like this:

[2016-12-16_14:16:19] Start VPN Client VMs restore
[2016-12-16_14:16:19] Connect vCenter Server
[2016-12-16_14:16:19] Connect Veeam Server
[2016-12-16_14:16:19] Backup Name: MyBackup-01
[2016-12-16_14:16:19] VMware Folder: MyFolder
[2016-12-16_14:16:19] VMs to restore ...

[2016-12-16_14:16:19] Restoreing VM: My-TestVM01
[2016-12-16_14:22:15] Restoreing VM: My-TestVM02
[2016-12-16_14:33:36] Restoreing VM: My-TestVM03
[2016-12-16_14:42:54] Restore Complete!
[2016-12-16_14:42:54] Poweron VMs ...

Name                 PowerState Num CPUs MemoryGB       
----                 ---------- -------- --------       
My-TestVM01			 PoweredOff 2        8,000          
My-TestVM02			 PoweredOff 2        8,000          
My-TestVM03			 PoweredOff 2        8,000          
[2016-12-16_14:42:57] Show Poweredon VMs ...

Name                 PowerState Num CPUs MemoryGB       
----                 ---------- -------- --------       
My-TestVM01			 PoweredOn  2        8,000          
My-TestVM02			 PoweredOn  2        8,000          
My-TestVM03			 PoweredOn  2        8,000          

[2016-12-16_14:42:59] StartTime: 12/16/2016 14:16:19
[2016-12-16_14:42:59]   EndTime: 12/16/2016 14:42:59
[2016-12-16_14:42:59]  Duration: 26.67 minutes
[2016-12-16_14:42:59] Complete!


2 thoughts on “Restore VMs via PowerShell

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.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.