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

How can I check if session is already closed - Stop-VESQLRestoreSession

Post by mphilippopoulos »

Hi,

In my script I use try/catch/finally logic to open a restore session, then close it. The script looks like this:

Code: Select all

try {
    …
    Start-VESQLRestoreSession -RestorePoint $restorepoint[-1];
    $session = Get-VESQLRestoreSession;
    …
    if ( $session[0] ) 
    {
	    Stop-VESQLRestoreSession -Session $session[0];
    }
}
catch {
…
}
finally {
    if ( $session[0] ) 
    {
	    Stop-VESQLRestoreSession -Session $session[0];
    }
    Disconnect-VBRServer;
}
When the code execution reaches the "finally" section, I get an error message that the session is already closed.
How can I check for that, so I don't try to stop the restore session if it is stopped already?
oleg.feoktistov
Veeam Software
Posts: 1918
Liked: 636 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: How can I check if session is already closed - Stop-VESQLRestoreSession

Post by oleg.feoktistov » 1 person likes this post

Hi Marios,

You have the same if statements in your try/finally sections. Moreover, your finally section will always be executed since $session variable wasn't redefined. Try the code below:

Code: Select all

finally {
      $exists = 'True'
      while ($exists -eq 'True') {
      $session = Get-VESQLRestoreSession | where {$_.Id -eq $session[0].Id}
      if (!$session) {
      Write-Host "Restore session with Id $($session[0].Id) has been stopped successfully."
      $exists = 'False'
}
     else {
       Start-Sleep -Seconds 15
       Write-Host "Awaiting for the session with Id $($session[0].Id) to stop."
    }
  }
}
Thanks,
Oleg
mphilippopoulos
Novice
Posts: 5
Liked: never
Joined: Jul 03, 2020 8:41 pm
Full Name: Marios Philippopoulos
Contact:

Re: How can I check if session is already closed - Stop-VESQLRestoreSession

Post by mphilippopoulos »

Hi Oleg, thank you for the info. I will give that a try.

Thanks!
Marios
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests