PowerShell script exchange
Post Reply
ShawnKPERS
Enthusiast
Posts: 56
Liked: 4 times
Joined: Apr 29, 2011 3:55 pm
Full Name: Shawn Nix
Contact:

File Level Restore Script and Getting a Folder Path

Post by ShawnKPERS »

I am trying to automate restoring a file from a windows VM and copy it out. I have a working Jerry rigged script I have been using but I was hoping to rehash an old post from six plus months ago:
http://forums.veeam.com/powershell-f26/ ... 18913.html

In the post I was trying to find a way to get a path to the file that is sitting in the restore session. At the time I was on v6 and tsightler posted some code that was supposed to do it in V7. I wrote around it and eventually upgraded to V7. I am now going back into the script to see if I can update the code with the recommended solution and find I am in the same boat where I don't see the “MountedDevices” nor “FirstDrive” options. Has anyone tried this and gotten it to work under V7?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by veremin »

For sure it worked at the time when the topic was created, as both Tom and I tested the script in our environment. What exact product version you're using? Also, you can query the $result variable, using Get-Member command, in order to see what particular properties are present.

Thanks.
tdewin
Veeam Software
Posts: 1775
Liked: 646 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by tdewin »

Was curious so tested in my lab. Maybe check if you have "enough permissions" (run powershell as administrator for example). Also check that you are getting back restore point e.g, run with the start-vbrwindowsfilerestore or check if you see your backup being mounted under c:\veeamflr

Code that is working

Code: Select all

asnp veeampssnapin
$result = Get-VBRRestorePoint | ? { $_.name -eq "exchange" -and $_.getbackup().jobtype -eq "Backup"} | sort Creationtime -descending | select -first 1 | Start-VBRWindowsFileRestore
$mountpoint = $($result.mountsession.mounteddevices | ? { $_.Driveletter -eq "C:" }).MountPoint
ls ("{0}\{1}" -f $mountpoint,"windows")
Image
ShawnKPERS
Enthusiast
Posts: 56
Liked: 4 times
Joined: Apr 29, 2011 3:55 pm
Full Name: Shawn Nix
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by ShawnKPERS » 1 person likes this post

Thanks for your help and testing, I think this was a PEBKAC problem. I was going off some of the code from the old post combined with my modified code to test and I was testing on the wrong variable. I was able to get .MountSession.MountedDevices to work once I changed the variable to what I had it labeled.

Just in case anyone is interested here is the code I came up with. We use it to get a the latest copy of our production database and copy it over to our one of our testing SQL servers to refresh our testing environment with fresh data. This script is helpful when restoring multiple files to the same destination.

Code: Select all

# Loads Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn

$vmName = "<Restore VM Name>"
$originalFiles = "M:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\DBFile.mdf", "L:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\DBFile_log.ldf"
$destPath = "\\TestSQLServer\mssql\DATA\"

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

# Copies the DB files to KTESTDB
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 to KTESTDB
    Copy-Item $file $destPath -Force
    }

# Stop the FLR process
Stop-VBRWindowsFileRestore  $restore


I ended up changing tsightler code and making "Start-VBRWindowsFileRestore" it's own variable so I could pull info like $result.CreationTimeString from the $result into an email that would let me know for sure what restore point was used to pull the copied files from. I also think this is what tripped me up when I was testing for .MountSession.MountedDevices code.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by veremin »

FYI, sometimes it even happens that widely-used parameter isn't present in particular environment. Such issues are typically related to the absence of the latest Windows Manager Framework. (most commonly, on the default version of powershell on Windows 2008R2). So, it's always recommended to install, at least, Windows Framework 3.0.

Thanks.
baltechnique
Novice
Posts: 8
Liked: 1 time
Joined: Sep 30, 2013 7:11 am
Full Name: Network Consulting
Contact:

[MERGED] : Restore Windows Guest Files in Powershell

Post by baltechnique »

Hi,

i needed to restore some Windows Guests Files from a Veeam backup. But i needed to do it by script.

So i wrote one, searching from among lot of scripts that did apply to V6 but not to V7.

Here is what i achieved to do:

Code: Select all

$Job = "myBackupJob"
$VM = "myVirtualMachine"
$Files = "C:\myFolderToRestoreFromBackup\"
$Destination = "C:\WhereToPutFilesOnThisServer\"

$result=Get-VBRBackup | where {$_.jobname -eq $Job} | Get-VBRRestorePoint | where {$_.name -eq $VM} | Sort-Object creationtime -Descending | Select-Object -First 1 | Start-VBRWindowsFileRestore
$flrmountpoint = ($result.MountSession.MountedDevices | ? {$_.DriveLetter -eq (split-path -qualifier $Files)})
$file = $flrmountpoint.MountPoint + (split-path -NoQualifier $Files)
Copy $file $Destination -recurse -force
Stop-VBRWindowsFileRestore $result
This script results to restore a folder.
It mounts the Backup from a Veeam Backup, copy the folder with replacement and then Stops the restore process to release the mount point.
chengeel
Lurker
Posts: 2
Liked: never
Joined: Mar 14, 2024 3:39 am
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by chengeel »

I tested above script with the file path exist in the restore vm server... but getting error complaining path not exist, and I noticed there was C:\VeeamFLR showing in the path as well as vloume1, which i did not specified. Any idea?

$Files = "C:\Temp\POBT_AsBuilt BaseLine\"
$Destination = "\\Servername\C:\Scripts\"

Copy : Cannot find path 'C:\VeeamFLR\<server name>_dc5155d9\Volume1\Temp\POBT_AsBuilt BaseLine\' because it does not exist.
At C:\Scripts\Untitled2.ps1:9 char:1
+ Copy $file $Destination -recurse -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\VeeamFLR\AIA...Built BaseLine\:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
david.domask
VeeaMVP
Posts: 1034
Liked: 278 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: File Level Restore Script and Getting a Folder Path

Post by david.domask »

Hi @chengeel,

Run the script line by line and check the result of this command:

$flrmountpoint = ($result.MountSession.MountedDevices | ? {$_.DriveLetter -eq (split-path -qualifier $Files)})

Check the path there and make sure it aligns with the mount point for the FLR. Veeam will always mount the disks from backup for FLR to C:\VeeamFLR. My guess is that something in the $flrmountpoint variable was built wrong.

I've not used the -qualifier parameter before but it looks like it just returns the Qualifier, which is like drive letter or reference path of the Files you're trying to restore, and it's trying to find which mount point it should be under VeeamFLR. I've not done it this way before, but I suspect that's where it's breaking as it might be returning a qualifier that isn't mapped in MountSession.MountedDevices, but print the outputs one by one and see what's incorrect.
David Domask | Director: Customer Care | Veeam Technical Support
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests