PowerShell script exchange
Post Reply
wgoodall
Enthusiast
Posts: 28
Liked: never
Joined: Aug 30, 2022 6:21 am
Full Name: Wesley Goodall
Contact:

File Restore PowerShell Script, Multiple VMs

Post by wgoodall »

Hi All,

I wonder if you can help, I need to have a script that will restore one file for every VM job to a specific folder, to test our VM backups monthly, this is currently a manual task.

Through help from this forum and some configuration changes of my own, I have put together the below script.. which works very well but is there a more elegant way to do this as I can see this script getting very cumbersome when dealing with a client with a lot of VMs.

Code: Select all

#Change VM to VMs required
$vmName = "VM1"
$vmName2 = "VM2"
$originalFiles = "C:\test"
#Change Path to match backup Job
$destPath = "C:\Veeam_Restore\TEST1\1"
$destPath2 = "C:\Veeam_Restore\TEST2\1"

# Loads the latest restore point for the servers
$result = Get-VBRBackup | Get-VBRRestorePoint | sort CreationTime -Descending | where {$_.VMName -eq "$vmName"} | select -First 1
$restore = $result | Start-VBRWindowsFileRestore
$result2 = Get-VBRBackup | Get-VBRRestorePoint | sort CreationTime -Descending | where {$_.VMName -eq "$vmName2"} | select -First 1
$restore2 = $result2 | Start-VBRWindowsFileRestore

# Copies the Files
foreach ($file in $originalFiles) {
    
    # Find the FLR mount point for the specific drive letter
    $flrmountpoint = ($restore.MountSession.MountedDevices | ? {$_.DriveLetter -eq (Split-Path $file -Qualifier)}).MountPoint

    # Build the path to the file via the FLR mount point
    $file = $flrmountpoint + (Split-Path $file -NoQualifier)

    #Copy/Restore the files
    Copy-Item $file $destPath -Force -recurse
    Copy-Item $file $destPath2 -Force -recurse
 
    }

# Stop the FLR process
Stop-VBRWindowsFileRestore  $restore
Stop-VBRWindowsFileRestore  $restore2
Exit
Thank you in advance for any help you can give

Kindest Regards

Wes
Mildur
Product Manager
Posts: 8735
Liked: 2294 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: File Restore PowerShell Script, Multiple VMs

Post by Mildur » 1 person likes this post

Hi Wes

Welcome to the R&D forum.

Instead of using "Start-VBRWindowsFileRestore" you could use our Data Integration API to publish the restore point and then copy the file. https://helpcenter.veeam.com/docs/backu ... ml?ver=110
But I don't expect it to be faster than the file restore command.

Also consider using SureBackup jobs. Restoring a single file does not ensure that your VMs can be powered on from a restore point and that application services (databases) will be started after restore.

Thanks
Fabian
Product Management Analyst @ Veeam Software
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File Restore PowerShell Script, Multiple VMs

Post by david.domask » 1 person likes this post

I would second @Mildur's suggestion and propose Surebackup. Your script undoubtedly works, but it just ensures that you can copy files, it doesn't ensure the machine can boot. Surebackup will save a lot of the labor for you.

If you are going to go with the script, I can consider a few thoughts.

1. Chaining Get-VBRBackup into Get-VBRRestorePoint as you are eats a lot of time because it's passing all backups to Get-VBRRestorepoint so you're fetching a ton of data twice. In larger environments, this part will take a very long time. Consider instead processing both commands and saving them to a variable first and using a picklist with Out-GridView so you can just pick the desired result. There are some unsupported .NET methods you can use to speed this up, but play around with Out-GridView to avoid having to hardcode VMs into the script.

2. To make the script a bit cleaner, write functions for your FLR sessions and just call the function with the appropriate variable passed. It will be easier to maintain.

3. Consider auto-generating the paths in powershell with New-Item and pass the VM names to make the directory.

4. To fully automate this, you can run a series of checks that just compares the original file from the backup to the copied file, and if the file size is the same, add it to a report which gets mailed out in the script (powershell has Send-MailMessage which can send an email straight from your script)

Keep in mind, I still recommend SureBackup; there is some networking overhead to get it set-up, but it will save you a lot of time and get the same result I think :)
David Domask | Product Management: Principal Analyst
wgoodall
Enthusiast
Posts: 28
Liked: never
Joined: Aug 30, 2022 6:21 am
Full Name: Wesley Goodall
Contact:

Re: File Restore PowerShell Script, Multiple VMs

Post by wgoodall »

Thank you @David and @Mildur

Really appreciate your suggestions

I will look into setting up SureBackup, but in the meantime, I would like to incorporate some of the changes you both suggested, I am not that strong on PowerShell, and really only put the script I shared together through taking parts from various scripts, and a lot of testing :)

Is there any chance you could put what you suggested into an example script, that would make it much easier for me to follow.

I will also review the Knowledge article mentioned by @Mildur

Thanks again to both of you for your time.
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests