PowerShell script exchange
Post Reply
mphilippopoulos
Novice
Posts: 5
Liked: never
Joined: Jul 03, 2020 8:41 pm
Full Name: Marios Philippopoulos
Contact:

How to "catch" Get-VBRApplicationRestorePoint errors

Post by mphilippopoulos »

Hi,

We are using Get-VBRApplicationRestorePoint to get app-aware restore points for SQL Server from a bunch of VMs.

We are getting the following error:

...
Get-VBRApplicationRestorePoint : A restore point VMName1 does not exist

Anyone have TRY/CATCH code for this sort of error?

Thanks,
Marios
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by oleg.feoktistov »

Hi Marios,

Can you, please, elaborate on what you are trying to achieve with error handling in your case?
Do you want a custom message displayed if this particular error is thrown or else?

Thanks,
Oleg
mphilippopoulos
Novice
Posts: 5
Liked: never
Joined: Jul 03, 2020 8:41 pm
Full Name: Marios Philippopoulos
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by mphilippopoulos »

Hi Oleg,

We are using the Get-VBRApplicationRestorePoint command inside a loop that cycles through a list of servers/databases and records the latest restore point for each.
We want to be able to log the error in a database table and continue on to the next server in the loop.
Also display the error (or a custom message) on the script output.

Thanks,
Marios
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by oleg.feoktistov » 1 person likes this post

The syntax of writing it to a database would depend on database type. You can take a look at Invoke-SqlCmd if it involves SQL server.
When it comes to try/catch construction, the script below would write a full error message to a file and throw custom error to the console output:

Code: Select all

try {
$rp = Get-VBRApplicationRestorePoint -Name 'VeeamSQL'
Start-VESQLRestoreSession -RestorePoint $rp
}
catch {
  if ($_.CategoryInfo.Category -eq 'InvalidData') {
    $_ > "C:\data.log"
    throw 'Invalid data passed' 
  }
}
Thanks,
Oleg
mphilippopoulos
Novice
Posts: 5
Liked: never
Joined: Jul 03, 2020 8:41 pm
Full Name: Marios Philippopoulos
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by mphilippopoulos »

Thank you! Will give that a try.
Anil1310
Novice
Posts: 5
Liked: never
Joined: Feb 11, 2021 12:00 pm
Full Name: Anil Babu
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by Anil1310 »

Hi All... We are using below ps script and it's working fine but if we getting any error , Try-Catch function is not collecting the actual error .

Code: Select all

$VMName="xxxxxxx"
$FilterHours="24"

function Run-SQLRestorePointReport {
begin
{
 $Username="xxxxx"
  $password=Get-Content "D:\password.txt" 
 $VBRServer = "xxxxx" #mention servername
Add-PSSnapin VeeamPsSnapin
$Vemmconn = Connect-VBRServer -Server $VBRServer -User $Username -Password $password
Add-PSSnapin "VeeamPSSnapIn" -ErrorAction SilentlyContinue
}
  process {
  try
  {
   if ($VMName -AND $FilterHours) {
   $SQLRPs = Get-VBRApplicationRestorePoint -Name $VMName -SQL | Where-Object { $_.CreationTime -ge ((Get-Date).AddHours(-$FilterHours)) }
    }
    elseif ($VMName) {
      $SQLRPs = Get-VBRApplicationRestorePoint -Name $VMName -SQL
    }
    elseif ($FilterHours) {
      $SQLRPs = Get-VBRApplicationRestorePoint -SQL | Where-Object { $_.CreationTime -ge ((Get-Date).AddHours(-$FilterHours)) }
    }
    else {
      $SQLRPs = Get-VBRApplicationRestorePoint -SQL
    }
  
}
    Catch
    {
   Write-Output ($_.Exception.Message) 
  }

    }
}

Disconnect-VBRServer
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by oleg.feoktistov »

Hi Anil,

Not all errors are passed to catch part by default. If the error is non-terminating, you need to tell powershell to pass it to catch and treat it as terminating. It can be done by setting $ErrorActionPreference = Stop in the beginning of your script.

Thanks,
Oleg
jhoughes
Veeam Vanguard
Posts: 279
Liked: 112 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Castle Rock, CO
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by jhoughes » 2 people like this post

It's actually better to set the ErrorAction parameter to 'Stop' for each cmdlet being run (within each if/elseif block), since this is a function containing that code.

Setting ErrorActionPreference may not pass that in & out of each scope properly.
Husband, Father, Solutions Architect, Geek Extraordinaire | @DenverVMUG, @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by oleg.feoktistov »

Agree with Joe here.
Anil1310
Novice
Posts: 5
Liked: never
Joined: Feb 11, 2021 12:00 pm
Full Name: Anil Babu
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by Anil1310 »

Thank you Oleg/Joe. Will try on this .
Anil1310
Novice
Posts: 5
Liked: never
Joined: Feb 11, 2021 12:00 pm
Full Name: Anil Babu
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by Anil1310 »

Hi Oleg/Joe.... I have implemented your suggestions and now Try-Catch function is collecting the actual error . but If we include function Run-SQLRestorePointReport { Try-Catch function not collecting the actual error.

Code: Select all

$VMName="XXXXXX"
$FilterHours="24"

#function Run-SQLRestorePointReport {

  $Username="XXXXXX"
  $password=Get-Content "D:\password.txt" 
 $VBRServer = "XXXXXX" #mention servername
Add-PSSnapin VeeamPsSnapin

$Vemmconn = Connect-VBRServer -Server $VBRServer -User $Username -Password $password

    $ErrorActionPreference = 'Stop'
    try {

    if ($VMName -AND $FilterHours) {

      $SQLRPs = Get-VBRApplicationRestorePoint -Name $VMName -SQL | Where-Object { $_.CreationTime -ge ((Get-Date).AddHours(-$FilterHours)) }

}
}

   catch {
       Write-Output ($_.Exception.Message) 
}

Disconnect-VBRServer 


Output:
A restore point XXXXXX does not exist
oleg.feoktistov
Veeam Software
Posts: 1912
Liked: 635 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by oleg.feoktistov »

Hi Anil,

Like Joe suggested above, set error action parameter for each cmdlet being run instead. The syntax is:

Code: Select all

Get-VBRApplicationRestorePoint -Name $VMName -SQL -ErrorAction Stop
Any cmdlet should support this common parameter.

Thanks,
Oleg
Anil1310
Novice
Posts: 5
Liked: never
Joined: Feb 11, 2021 12:00 pm
Full Name: Anil Babu
Contact:

Re: How to "catch" Get-VBRApplicationRestorePoint errors

Post by Anil1310 »

Thanks Oleg. It's working now.
Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests