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

PowerShell script exchange

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

Veeam Logoby StefanB » Wed Mar 08, 2017 7:24 am

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
StefanB
Influencer
 
Posts: 11
Liked: 3 times
Joined: Tue Oct 15, 2013 7:20 am

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

Veeam Logoby tdewin » Wed Mar 08, 2017 9:00 am

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)
tdewin
Veeam Software
 
Posts: 1110
Liked: 391 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

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

Veeam Logoby StefanB » Wed Mar 08, 2017 11:16 am

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?
StefanB
Influencer
 
Posts: 11
Liked: 3 times
Joined: Tue Oct 15, 2013 7:20 am

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

Veeam Logoby tdewin » Wed Mar 08, 2017 11:33 am 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" }
tdewin
Veeam Software
 
Posts: 1110
Liked: 391 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

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

Veeam Logoby StefanB » Wed Mar 08, 2017 12:54 pm

Great! Thank you. Works perfect.
StefanB
Influencer
 
Posts: 11
Liked: 3 times
Joined: Tue Oct 15, 2013 7:20 am


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 1 guest