PowerShell script exchange
Post Reply
hmurat
Influencer
Posts: 11
Liked: never
Joined: Nov 15, 2018 9:48 pm
Contact:

Capture VBRSession Error messages?

Post by hmurat »

Hello community!

I'm automating the restore of some databases on a server so it can be done with a few clicks, and I've been pretty successful so far. The part I'm somewhat stuck on is how to retrieve an error when a restore fails for whatever reason.

Here's my workflow:
- Start-VBRDatabaseRestore and collect the $sessionId
- Get-VBRRestoreSession -Id $sessionId every 15s and loop until the `Result` is -ne "None", meaning its finished in some fashion
- ??? Get Error ??? - <--- this is the part I can't yet figure out
- Report Error

On the RestoreSession object, there's a JobId, but it appears to be used in conjunction with the Export-VBRLogs method. Does anyone have any suggestions? In the Veeam Backup and Replication list of Sessions, I can see that the Restore Session has a Log Message that's marked as failed (an artificial disk space shortage ;) ) but I'd really like to report that through the automatic tool.

Thanks in advance!

Hugh
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Capture VBRSession Error messages?

Post by veremin »

Try to query session log and check titles of updated records. The error message should be reported there:

Code: Select all

asnp VeeamPSSnapin
$Session = Get-VBRRestoreSession | ? {$_.Jobid -eq "Job Id"}
$Session.Logger.GetLog().UpdatedRecords.Title
Thanks!
hmurat
Influencer
Posts: 11
Liked: never
Joined: Nov 15, 2018 9:48 pm
Contact:

Re: Capture VBRSession Error messages?

Post by hmurat »

Thanks very much! I went with something like this:

Code: Select all

$errors = $session.Logger.GetLog() | ? ( $_.Status -eq "EFailed" } | Select-Object Title
Write-Warning "Errors: $($errors.Title -Join "`n")"
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Capture VBRSession Error messages?

Post by veremin »

You've missed few properties, actually. So, your code should look similar to the following:

Code: Select all

asnp VeeamPSSnapin
$Session = Get-VBRRestoreSession | ? {$_.Jobid -eq "Job Id"}
$errors = $session.Logger.GetLog().UpdatedRecords | ? {$_.Status -eq "EFailed"} | Select-Object Title
Write-Warning "Errors: $($errors.Title -Join "`n")"
Thanks!
hmurat
Influencer
Posts: 11
Liked: never
Joined: Nov 15, 2018 9:48 pm
Contact:

Re: Capture VBRSession Error messages?

Post by hmurat »

Oh yeah, that's not the whole thing. This is the looping part that I'm doing to wait for the restore job to complete:

Code: Select all

# store the restore sessionId
$sessionId = $session.ID
Write-Host "SessionId - $sessionId"

do {
  Write-Host "Sleeping for 15s..."  
  Start-Sleep -s 15

  Write-Host "Get latest session..."
  $session = Get-VBRRestoreSession -Id $sessionId
  
# loop again while the job has no Result, which means it isn't finished yet
} while ($session.Result -eq "None")

if ($session.Result -ne "Success") {
	$log = $session.Logger.GetLog()
	$errors = $log.UpdatedRecords | Where { $_.Status -eq "EFailed" } | Select-Object Title
	
	Write-Warning "Something bailed restoring $source_db_name"
	Write-Warning "Veeam Errors: $($errors.Title -Join "`n")"
}
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Capture VBRSession Error messages?

Post by veremin »

Looks good. By the way, instead of re-assigning session within and checking its result within a cycle, you can make use of IsRestoreSessionCompleted() method. It indicates whether the session is finished or not. Thanks!
hmurat
Influencer
Posts: 11
Liked: never
Joined: Nov 15, 2018 9:48 pm
Contact:

Re: Capture VBRSession Error messages?

Post by hmurat »

Yeah, that's way more preferable than getting a new session each time. I didn't see that method in the documentation (and didn't inspect the object itself :P ). Thanks for the help!
hmurat
Influencer
Posts: 11
Liked: never
Joined: Nov 15, 2018 9:48 pm
Contact:

Re: Capture VBRSession Error messages?

Post by hmurat »

I discovered that the Start-VBRSQLDatabaseRestore returns a VBRSession object which doesn't have the IsRestoreSessionCompleted() method. I modified my script to Get-VBRRestoreSession a single time, but the IsRestoreSessionCompleted() method doesn't appear to update when I retrieve the session manually, so I've gotta grab the session repeatedly.

Would I need to cast the original VBRSession (from Start-VBRSQLDatabaseRestore) to a VBRRestoreSession to make this method available?
veremin
Product Manager
Posts: 20283
Liked: 2258 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Capture VBRSession Error messages?

Post by veremin » 1 person likes this post

Ah, in this case I think you will need to re-assign session repeatedly, until it ends - the dynamic method I talked about is not present in VBRSession object. Thanks!
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests