PowerShell script exchange
Post Reply
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

List failed servers

Post by EvoTom »

We have 6 Veeam Jobs each containing about 10 or so servers. What I am trying to do eventually in PowerShell is create a script that will run once a day and delete oracle archive redo logs if the backup job for the server was successful. If it is unsuccessful I want it to send an e-mail or something like that.

Anyway I am failing at the first hurdle which is to get a list of servers who's backup have failed.

I can get a list of failed jobs by running:

Code: Select all

Get-VBRJob | ? {$_.GetLastResult() -eq "Failed"}
and I can get the servers in that job by piping that into Get-VBRJobObject

Code: Select all

Get-VBRJob | ? {$_.GetLastResult() -eq "Failed"} | Get-VBRJobObject
what I can't seem to do is list which server(s) in the group of 10 or so was the one(s) that failed. I dont see a method for Get-VBRJobObject that will tell me.

Any ideas?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

Something like this should answer your requirements:

Code: Select all

asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job" 
$Session = $Job.FindLastSession()
$Session.GetTaskSessionsByStatus("Failed") 
This script will list all VMs which failed to be backed up during the last run of the given job.

Hope this helps.
Thanks.
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

That's perfect. Thank you!

I will probably have some more questions as I move forward with creating this script.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

I will probably have some more questions as I move forward with creating this script.
Don’t hesitate to let us know, should any additional help be needed.

Thanks.
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

Is there a way to do it so that I can include all Jobs and just get a list of failed servers?

I have tried

Code: Select all

$Job = Get-VBRJob
$Session = $Job.FindLastSession()
$Session.GetTaskSessionsByStatus("Failed")}
but when i get to the $Session = $Job.FindLastSession() is throws an error.

Is there a way just to list the failed servers?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

Is there a way to do it so that I can include all Jobs and just get a list of failed servers?
Yep, I believe so.

Nevertheless, for doing it some sort of cycle should be implemented.

Code: Select all

asnp VeeamPSSnapin
foreach ($Job in Get-VBRJob | ? {$_.JobType -eq "Backup" -and $_.GetLastResult() -eq "Failed"})
{
    foreach ($failedVM in $Job.FindLastSession().GetTaskSessionsByStatus("Failed"))
    {
       write-host $failedVM.Name
    }
} 
Please be aware that It’s nothing but a rough example; feel free to modify it however you want.

Hope this helps.
Thanks.
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

This is a mostly a learning exercise (new to PowerShell) for me and I really appreciate the help. Just have one more question. I am now trying to get an e-mail sent for each failed VM. I get e-mail fine but the $srv.Name is not being passed to the -Body argument properly.

Here's the code:

Code: Select all

### Return servers which have failed.
$fails=foreach ($Job in Get-VBRJob | ? {$_.JobType -eq "Backup" -and $_.GetLastResult() -eq "Failed"})
	{
		foreach ($failedVM in $Job.FindLastSession().GetTaskSessionsByStatus("Failed"))
		{
			write-output $failedVM
		}
	}

### Mail settings
$MailSrv = "mailserver.company.com"
$MailFrom = "veeam@company.com"
$MailTo = "me@company.com"
$MailSbjt = "Veeam Job Failure Report"

### Send a mail for each failed server.
foreach ($srv in $fails)
	{
		$srv.Name
		Send-MailMessage -SmtpServer $MailSrv  -From $MailFrom -To $MailTo -Subject $MailSbjt -Body "$srv.Name failed"
	}
$srv.Name prints the server names to the screen fine. It's just that the e-mail body looks like this:
Veeam.Backup.Core.CBackupTaskSession.Name failed
Not really sure why...

Thanks again for the help.
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

Don't worry. I have figured it out. I don't think the -Body argument liked having a variable property so I just created a new variable to hold it. Send message bit looks like this now:

Code: Select all

foreach ($srv in $fails)
	{
		$body=$srv.Name
		Send-MailMessage -SmtpServer $MailSrv  -From $MailFrom -To $MailTo -Subject $MailSbjt -Body "$body failed"
	}
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

Think I actually getting somewhere now with this :D

Okay, I have a job with 5 servers in. One of the servers in the job fails causing the .GetLastResult of Get-VBRJob to equal "Failed". How do I list the servers in that Job that completed successfully.

Going back to earlier I tried this:

Code: Select all

asnp VeeamPSSnapin
$Job = Get-VBRJob -name "Name of your job"
$Session = $Job.FindLastSession()
$Session.GetTaskSessionsByStatus("Success") 
I should find one server as we have a Job where one server has failed and the other one is successful. I also tried with the loop but again got no results:

Code: Select all

foreach ($Job in Get-VBRJob | ? {$_.JobType -eq "Backup" -and $_.GetLastResult() -eq "Failed"})
{
    foreach ($failedVM in $Job.FindLastSession().GetTaskSessionsByStatus("Success"))
    {
       write-host $failedVM.Name
    }
} 
Any ideas on this one?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

I’ve just reproduced same situation in my test environment and the following script seems to work properly:

Code: Select all

Add-PSSnapin VeeamPSSnapin
foreach ($Job in Get-VBRJob | ? {$_.JobType -eq "Backup" -and $_.GetLastResult() -eq "Failed"})
{
    foreach ($failedVM in $Job.FindLastSession().GetTaskSessionsByStatus("Success"))
    {
       write-host $failedVM.Name
    }
} 

So, if i were you, I’d doublecheck whether the given job failed during the last session and whether the VM you're talking about was backed up successfully during this session.

Hope this helps.
Thanks
EvoTom
Enthusiast
Posts: 26
Liked: 1 time
Joined: Jul 11, 2012 10:18 am
Contact:

Re: List failed servers

Post by EvoTom »

Ah Ok. I think I have seen why it's not working for me. The first time the job runs the two servers run, one fails the other is successful. Then the job retries the failed server 3 times. This means that the last session for that job only the failed server runs (and fails).

Any ideas of getting round this?
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

Hmm. May be something like this:

Code: Select all

asnp VeeamPSSnapin
$vbrsessions = Get-VBRBackupSession | ?{$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).adddays(-1) -and $_.result -eq "Failed"}
foreach ($session in $vbrsessions)
{
    foreach ($failedVM in $session.GetTaskSessionsByStatus("Success"))
    {
       write-host $failedVM.Name
    }
}  
This script takes all backup sessions that have failed during the day and lists VMs that have been backed up successfully within them. However, under certain circumstances there might be a sight effect that some VM will be listed several times.

Hope this helps.
Thanks.
aaronmnzs
Lurker
Posts: 1
Liked: never
Joined: Feb 10, 2015 12:17 pm
Full Name: Aaron Menezes
Contact:

Re: List failed servers

Post by aaronmnzs »

is there a way wherein I could also list the error (Reason of failure) along with the failed VMs
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

Try the following script and see whether it's something you're looking for:

Code: Select all

$vbrsessions = Get-VBRBackupSession | ?{$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).adddays(-1) -and $_.result -eq "Failed"}
foreach ($session in $vbrsessions)
{
    foreach ($failedVM in $session.GetTaskSessionsByStatus("Failed"))
    {
       write-host $failedVM.Name, $FailedVM.info.reason
    }
}  
Thanks.
baum1
Lurker
Posts: 1
Liked: never
Joined: Jun 09, 2015 1:16 pm
Full Name: Christian Baumann
Location: Karlsruhe, Germany
Contact:

Re: List failed servers

Post by baum1 »

Hi, sorry for picking up this old thread!

We're using B&R 8 with latest SP and Windows 2012 Standard Server.

What I'm trying is to get a list of all failed VMs with the Veeam PSSnapin, we solved it so far that we'll getting a list of all failed VMs but with lots of information. We only need the Names of the failed ones. Is that possible with power shell? We're not very familiar with power shell..

Cheers
Vitaliy S.
VP, Product Management
Posts: 27055
Liked: 2710 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: List failed servers

Post by Vitaliy S. »

Can't comment on the PowerShell script examples, but if you're not very familiar with scripting, you can use Veeam ONE (part of Availability Suite) to report on failed VMs automatically through predefined report templates.
veremin
Product Manager
Posts: 20270
Liked: 2252 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: List failed servers

Post by veremin »

Hi, Christian,

The latest script provided by me lists only VM names and failure reasons. If you don't want the latter information, feel free to remove $FailedVM.info.reason. Or you're saying that the script doesn't work in your case?

Thanks.
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests