PowerShell script exchange
Post Reply
StefanB
Influencer
Posts: 13
Liked: 3 times
Joined: Oct 15, 2013 7:20 am
Contact:

Check backup status of vm in job - if failed - restart vm

Post by StefanB »

Hello, maybe someone can help me with the correct code to run any command (e.g. restart whole vm) if the last backup of specific vm failed in the scheduled backup job.

Already found the code to check the last Status with findlastsession/gettasksessions:

Code: Select all

asnp VeeamPSSnapin
$VMName = "Name_of_VM"
$Job = Get-VBRJob -Name "Name_of_Backup_Job"
$Session = $Job.FindLastSession()
$Tasks = $Session.GetTaskSessions()
$Tasks | ?{$_.Name -eq $VMName} | %{write-host $_.Name ":" $_.Status}
Output is e.g.
Name_of_VM : Failed

Need something like this, but for a specific vm if status is 'failed' - instead of a whole job.

Code: Select all

asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job"
If ($job.GetLastresult() -eq "Warning" -or $job.GetLastresult() -eq "Success")
{
Place your commands here that you want to start
} Else {}
Thanks for any help.
Stefan
tdewin
Veeam Software
Posts: 1818
Liked: 655 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Check backup status of vm in job - if failed - restart v

Post by tdewin »

Didn't test it but I believe you can do something like this

Code: Select all

$Tasks | ? {$_.Name -eq $VMName -and $_.Status -eq "Failed"} | %{
write-host $_.Name ":" $_.Status
}
or if you want both failed and warning

Code: Select all

$Tasks | ? {$_.Name -eq $VMName -and $_.Status -in @("Failed","Warning")} | %{
write-host $_.Name ":" $_.Status
}
Of course it depends. If you want to do something in both case (if/else), you will need a different construct. If however, you want to do something when a VM fails, you can use this "for loop" construction, because, the VM should only occur once in the job itself and thus the command runs 0 (no failed session for vm) or 1 time (failed session for vm)
StefanB
Influencer
Posts: 13
Liked: 3 times
Joined: Oct 15, 2013 7:20 am
Contact:

Re: Check backup status of vm in job - if failed - restart v

Post by StefanB »

Yep, this will work.

Can anyone help me with the code if we really want to work with both case (if/else) and the needed code construct?
tdewin
Veeam Software
Posts: 1818
Liked: 655 times
Joined: Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin
Contact:

Re: Check backup status of vm in job - if failed - restart v

Post by tdewin » 1 person likes this post

Something like this

Code: Select all

if($($Tasks | ? {$_.Name -eq $VMName -and $_.Status -eq "Failed"}) -ne $null) {
 write-host "Failed"
} else {
 write-host "All good"
}
I actually test this code quickly to see if chrome is missing so it should work:

Code: Select all

if ($(get-process | ? { $_.Name -match "chrome"}) -ne $null) { write-host "running" } else { write-host "not running" }
StefanB
Influencer
Posts: 13
Liked: 3 times
Joined: Oct 15, 2013 7:20 am
Contact:

Re: Check backup status of vm in job - if failed - restart v

Post by StefanB »

Great! Thank you. Works perfect.
Post Reply

Who is online

Users browsing this forum: R.Timmons and 15 guests