PowerShell script exchange
Post Reply
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Problem with restore using powershell

Post by pwinterbourne »

Hi,
I am trying to set up a script to restore a file but its failing due to the session not being correct for some reason.

my script is:

Code: Select all

$restore_point = Get-VBRBackup -Name "Backup Job 1E" | Get-VBRRestorePoint -Name ServerX | Sort-Object –Property CreationTime –Descending | Select-Object -First 1
$restore_start = Start-VBRWindowsFileRestore -RestorePoint $restore_point -Reason "Backup Integrity Check"
$session = Get-VBRRestoreSession
$items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRestoreTest\ServerXDrive.txt"
Copy-VBRWindowsGuestItem -Item $items -TargetFolder "C:\VeeamRestoreCheck\ServerX" -Session $session -KeepPermissionsAndOwnership -RunAsync
Stop-VBRWindowsFileRestore $restore_start
When i do this line by line in powershell, it fails on the $items line:

Code: Select all

$items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRestoreTest\ServerXDrive.txt"
Get-VBRWindowsGuestItem : Cannot convert 'System.Object[]' to the type 'Veeam.Backup.Core.CRestoreSession' required by
parameter 'Session'. Specified method is not supported.
At line:1 char:43
+ $items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRes ...
+                                           ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-VBRWindowsGuestItem], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Veeam.Backup.PowerShell.Cmdlets.GetVBRWindowsGuestItem
I just want the restore to work on the current session and restore the file from the latest backup.

thanks

Paul
oleg.feoktistov
Veeam Software
Posts: 1964
Liked: 650 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Problem with restore using powershell

Post by oleg.feoktistov »

Hi Paul,

Please check this line:

Code: Select all

$session = Get-VBRRestoreSession
The exception means that $session holds an array of session objects, but Get-VBRWindowsGuestItem cmdlet accepts only one. So you would need to filter out the output of Get-VBRRestoreSession just to one session to make it work.

Best regards,
Oleg
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Thanks Oleg,

sorry to be a bit stupid but how would I get the latest current session from the array?
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

$session = Get-VBRRestoreSession| Sort-Object –Property CreationTime –Descending | Select-Object -First 1
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

that worked, thanks for the help
Mildur
Product Manager
Posts: 9237
Liked: 2430 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: Problem with restore using powershell

Post by Mildur »

With your code, you will get the last restore session. What if one of your colleagues started a second restore session right after your script? No one can guarantee you that the last restore session was the one you have started with the script.

Code: Select all

$session = Get-VBRRestoreSession| Sort-Object –Property CreationTime –Descending | Select-Object -First 1
I recommend an other approach for saving the restore session in the variable. You can store the restore session to the variable directly when it is started:

Code: Select all

$session = Start-VBRWindowsFileRestore -RestorePoint $restore_point -Reason "Backup Integrity Check"
Please try the following script for the entire process:

Code: Select all

$restore_point = Get-VBRBackup -Name "Backup Job 1E" | Get-VBRRestorePoint -Name ServerX | Sort-Object –Property CreationTime –Descending | Select-Object -First 1
$session = Start-VBRWindowsFileRestore -RestorePoint $restore_point -Reason "Backup Integrity Check"
$items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRestoreTest\ServerXDrive.txt"
Copy-VBRWindowsGuestItem -Item $items -TargetFolder "C:\VeeamRestoreCheck\ServerX" -Session $session -KeepPermissionsAndOwnership -RunAsync
Stop-VBRWindowsFileRestore $session
Best,
Fabian
Product Management Analyst @ Veeam Software
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

unfortunately, when running the scripts as the full .ps1 file it fails on both yours and mine. If I run each command manually it works ok.

On my previous script it gives:

Code: Select all

PS C:\VeeamRestoreCheck> .\Restorecheckbasic.ps1
Get-VBRWindowsGuestItem : Cannot find restore session with the specified Id: a0bbdbac-0dc0-4fad-a72c-5af810ddc3c8.
At C:\VeeamRestoreCheck\Restorecheckbasic.ps1:10 char:10
+ $items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRes ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-VBRWindowsGuestItem], Exception
    + FullyQualifiedErrorId : VBRGetGuestItemId,Veeam.Backup.PowerShell.Cmdlets.GetVBRWindowsGuestItem

Copy-VBRWindowsGuestItem : Cannot validate argument on parameter 'Item'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\VeeamRestoreCheck\Restorecheckbasic.ps1:11 char:32
+ Copy-VBRWindowsGuestItem -Item $items -TargetFolder "C:\VeeamRestoreC ...
+                                ~~~~~~
    + CategoryInfo          : InvalidData: (:) [Copy-VBRWindowsGuestItem], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Veeam.Backup.PowerShell.Cmdlets.CopyVBRWindowsGuestItem
david.domask
Veeam Software
Posts: 1653
Liked: 397 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with restore using powershell

Post by david.domask »

Hi @pwinterbourne,

Can you post line 10? That failed to return the GuestItem Object for some reason and it's not clear what operation failed (you can check the powershell log from VBR/Console machine in %localappdata%/Veeam folder, you'll see VeeamPowershell.log file.

LIne 11 failed because of the failure at line 10; the $items variable was NULL, so we just need to find out why that failed in your script and on what.
David Domask | Product Management: Principal Analyst
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Hiya,

Line 10 is

Code: Select all

Copy-VBRWindowsGuestItem -Item $items -TargetFolder "C:\VeeamRestoreCheck\Server1" -Session $session -KeepPermissionsAndOwnership
the full script is:

Code: Select all

$restore_point = Get-VBRBackup -Name "Backup Job 1E" | Get-VBRRestorePoint -Name Server1 | Sort-Object –Property CreationTime –Descending | Select-Object -First 1
$restore_start = Start-VBRWindowsFileRestore -RestorePoint $restore_point -Reason "Backup Integrity Check"
$session = Get-VBRRestoreSession| Sort-Object –Property CreationTime –Descending | Select-Object -First 1
$items = Get-VBRWindowsGuestItem -Session $session -Path "C:\VeeamRestoreTest\Server1CDrive.txt"
Copy-VBRWindowsGuestItem -Item $items -TargetFolder "C:\VeeamRestoreCheck\Server1" -Session $session -KeepPermissionsAndOwnership
Stop-VBRWindowsFileRestore $restore_start
if I run each line in the powershell console it works fine but if i call the script i.e. ./script.ps1 it fails with the error in my previous post
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

I think line 10 is failing because it is passed the $session from the previous line but that is not being passed for some reason (or it is not finding the one it is being passed).
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Code: Select all

[27.01.2023 17:05:40] <14> Info         Instance of 'GetVBRWindowsGuestItem' was created with Id 'd196bb7c8b4c4b85990721838baedec6'
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] ------- Begin Processing of Veeam PowerShell -------
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Running cmdlet: Veeam.Backup.PowerShell.Cmdlets.GetVBRWindowsGuestItem
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Running command name: Get-VBRWindowsGuestItem
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Cmdlet script name: C:\VeeamRestoreCheck\Restorecheckbasic2.ps1, line number: 9, offset: 10
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Cmdlet pipeline length: 1, position: 1
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] BoundParameters: 'Session' - 'Veeam.Backup.Core.CRestoreSession'
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] BoundParameters: 'Path' - 'C:\VeeamRestoreTest\Server1CDrive.txt'
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] --------------- Command session out ---------------
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] ------- Processing of Veeam PowerShell -------
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Executing license check...
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] Preparing parameters...
[27.01.2023 17:05:40] <14> Error        The pipeline has been stopped. (System.Management.Automation.PipelineStoppedException)
[27.01.2023 17:05:40] <14> Error           at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
[27.01.2023 17:05:40] <14> Error           at System.Management.Automation.Cmdlet.ThrowTerminatingError(ErrorRecord errorRecord)
[27.01.2023 17:05:40] <14> Error           at Veeam.Backup.PowerShell.Private.PSUserInteraction.ThrowTerminatingError(Exception ex, String errorDescriptionId)
[27.01.2023 17:05:40] <14> Error           at Veeam.Backup.PowerShell.Cmdlets.GetVBRWindowsGuestItem.PrepareParameters()
[27.01.2023 17:05:40] <14> Error           at Veeam.Backup.PowerShell.Cmdlets.PSCmdletBase.ProcessRecord()
[27.01.2023 17:05:40] <14> Info         [d196bb7c8b4c4b85990721838baedec6] ------- End Processing of Veeam PowerShell -------
howartp
Enthusiast
Posts: 78
Liked: 10 times
Joined: Jun 08, 2013 10:52 am
Full Name: Peter Howarth
Contact:

Re: Problem with restore using powershell

Post by howartp » 1 person likes this post

Before you carry on, I’d go back to @mildur’s reply above.

The script you’re using gets the ‘last’ session, but he correctly suggests grabbing the session when you start it (which actually you already are in $restore_start but you’re ignoring it and trying to get it again in $session).

Then retry and see if you still get the latest errors you’ve posted.
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Thank you all for your help. I have managed to get this just about working now. The only last issue I have is that when mounting some volumes they are coming up as Volume0:\ or Volume1:\ instead of their drive letter. When I try to assign my variable $items = Get-VBRWindowsGuestItem -Session $session -Path "Volume0:\VeeamRestoreTest\Server1CDrive.txt" it doesnt work. I have tried a myriad of different methods to be able to access the drive letter including what the drive letter should be but to no avail. Do you know what the path should be when the drive letter is replaced for a volume?
david.domask
Veeam Software
Posts: 1653
Liked: 397 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with restore using powershell

Post by david.domask »

Hi @pwinterbourne,

Get-VBRWindowsGuestItem will use the mount points for information, and it has several search options:

-Name == Specify a file and it will search the mount point for it and return all files that match the expression
-RecursiveSearch == search all directories recursively

You'd want to specify folders or files here and from there pass these to Start-VBRWindowsGuestItemRestore for the -Item parameter
David Domask | Product Management: Principal Analyst
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Thanks for that. I am basically trying to write a script that does a test restore of a file on all of our drives/servers/backup jobs daily. We had in issue recently where a vm was supposedly being backed up but when we came to mount a drive to restore the file, it wouldn't mount and was corrupted. This was an effort to make sure we do not come across that again. The issue is that we have some drives that are 1-2TB and we have around 50 drives being backed up in total. The script roughly took half an hour when I was putting in the paths (although some failed as the drive letter is not there and it just says volume). If I have to go down the route of searching through the drive then this could take a long time. I am going to try creating the directory on the server with a 1 in front of it so that it is at the top which may speed things along. I still cannot understand why the drive letter is missing when mounting the volume for a restore and also would be good to know if it is possible to actually access that volume using the powershell commands. Maybe it is not possible. It would also be good to know how we can make the drive letter appear in the mounted back up.

I appreciate all of your help and comments, they have been very useful!
Mildur
Product Manager
Posts: 9237
Liked: 2430 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: Problem with restore using powershell

Post by Mildur »

It would also be good to know how we can make the drive letter appear in the mounted back up.
Do you protect the operating system volumes with the same job?
Product Management Analyst @ Veeam Software
david.domask
Veeam Software
Posts: 1653
Liked: 397 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Problem with restore using powershell

Post by david.domask »

Just my input now that I get your strategy; I would actually either utilize SureBackup jobs with a custom script to PSRemote to the machine in Surebackup Lab and do your checks that way, or I would use the Data Integration API and mount the backup content to a known machine that runs the script and do your checks that way.

I think Surebackup + a script is probably going to be less hassle, and you just need to get over the PSRemoting part.

> I still cannot understand why the drive letter is missing when mounting the volume for a restore and also would be good to know if it is possible to actually access that volume using the powershell commands. Maybe it is not possible. It would also be good to know how we can make the drive letter appear in the mounted back up.

It's because when the disks get mounted, to access them we need to use the mount paths; we cannot guarantee the same drive letters are free on the mount server, so they don't get mounted as accessible drives with drive letters, they're just mounted to a specific mount point. Start-VBRWindowsFileLevelRestore will actually return the FileRestore object which has the MountSession property; if your machine was backed up with its system volume, you can see the original machine drive mapping and the mount point mapping under MountSession.MountedDevices:

Code: Select all

PS C:\Users\Administrator> $FLRSess.MountSession


RestoreSessionInfo : Veeam.Backup.Model.CRestoreSessionInfo
MountedDevices     : {MountPoint: [C:\VeeamFLR\ddom-veeam-rb3__4994270a\Volume0], DriveLetter: [], DeviceNames:
                     [Volume{c357dca3-5a15-464c-8dba-bc51911619cc}], Attributes: [System], MountPoint:
                     [C:\VeeamFLR\ddom-veeam-rb3-buch_4994270a\Volume1], DriveLetter: [C:], DeviceNames:
                     [C:,Volume{04bcfded-2e1f-4bee-b4c0-c613cabfd6b2}], Attributes: [System]}
AsmReplacementInfo : {}
FlrMountFolder     :
MetadataProvider   : Veeam.Backup.Core.CEmptyMetadataProvider



PS C:\Users\Administrator> $FLRSess.MountSession.MountedDevices


MountPoint      : C:\VeeamFLR\ddom-veeam-rb3__4994270a\Volume0
DeviceNames     : {Volume{c357dca3-5a15-464c-8dba-bc51911619cc}}
IsAccessible    : True
DriveLetter     :
Attributes      : System
DriveLetterChar :

MountPoint      : C:\VeeamFLR\ddom-veeam-rb3_4994270a\Volume1
DeviceNames     : {C:, Volume{04bcfded-2e1f-4bee-b4c0-c613cabfd6b2}}
IsAccessible    : True
DriveLetter     : C:
Attributes      : System
DriveLetterChar : C
From there you can know how to either manually pull files or check them on the mount points directly on the mount server.

But if it were me, I'd be using Surebackup and checking it with a script that connects to the Surebackup VM and does tests.
David Domask | Product Management: Principal Analyst
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Again, I really appreciate the help and input. I too would use Sure backup but we are not currently licensed for it I don't believe and in the past, we had the Enterprise license but the Sure Backup performance was very slow mainly due to various prerequisite servers that needed booting up. Admittedly, it was a while ago and I may end up going back there to try it again. We are planning on moving over to the new licensing model this year but it was more expensive than the perpetual model we are currently on (and costs are tight at the moment) so we have not moved yet.

I will persevere with the powershell option for now.
pwinterbourne
Enthusiast
Posts: 29
Liked: 1 time
Joined: Jan 10, 2012 10:38 am
Full Name: Paul Winterbourne
Contact:

Re: Problem with restore using powershell

Post by pwinterbourne »

Just to say, no, not all vm's disks are in the same job. This is because we have to juggle when copying backups to USB drives every day otherwise all the jobs wouldnt fit on the USB drives.
Mildur
Product Manager
Posts: 9237
Liked: 2430 times
Joined: May 13, 2017 4:51 pm
Full Name: Fabian K.
Location: Switzerland
Contact:

Re: Problem with restore using powershell

Post by Mildur »

If I remember correctly you need to add your operating system drive to the backup job. If not, volumes won‘t show there drive letter in the restore explorer. This information is stored on the operating system volume.
Product Management Analyst @ Veeam Software
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests