PowerShell script exchange
Post Reply
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

execute PowerShell script only on failures

Post by Kazz »

Hi,

I need to run a PowerShell script only if one of my Veeam backup / copy or replica jobs failed. I have over 100 jobs that run non stop (Veeam is managing jobs for US, Europe and Asia)

I am doing the exact opposite of what is being discussed in this old theat. -> vmware-vsphere-f24/job-scripts-run-only ... 32344.html

Glancing over the discussion in the forum post above, I see people were implementing delays as a workaround for PS scripts running at the end of the job. Do I still need a delay in V12 or Veeam is posting job status before the script is executed?

If I have 2 jobs Fail at the exact same time would Get-VBRSession return the failure only for the last job ?

Thank you
david.domask
Veeam Software
Posts: 2566
Liked: 603 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: execute PowerShell script only on failures

Post by david.domask » 1 person likes this post

Hi @Kazz,

Correct -- post-job scripts will run as the very tail end of the job; can you share a bit what actions you want to take with the post-job script if there are failures?

I would recommend consult here before implementing, but best way I can think of is to use the following code to find the invoking process (the backup job), and then poll the session data:

Code: Select all

$ScriptPID = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $PID}
$ParentProc = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $ScriptPID.ParentProcessId}
$JobID = $ParentProc.CommandLine.Split()[7].Trim('"')
$Job = Get-VBRJob | Where-Object {$_.id -eq $JobID}
This will return the CBackupJob object, which can be passed to Get-VBRBackupSession (pipe the cmdlet to Where-Object {$_.JobID -eq $job.id}) and sort the returned sessions by CreationTime -Descending to get the most recent session.

But I am curious what action you want to take based on the result of the job run; maybe there's a better way.
David Domask | Product Management: Principal Analyst
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: execute PowerShell script only on failures

Post by Kazz » 1 person likes this post

Hi @david.domask

Thank you for getting back. The idea is to fire a Teams message to a channel on a job failure. I have over 100 notifications from Veeam daily and most of them are successful, but occasionally there would be a failure that someone would need to address. So we are looking at Teams notifications as a potential option to get away from relaying on emails and rules associated with them to filter out alerts that need to be addressed.

Please let me know if I understand this correctly.

If I have a failed job and a second later I have a successful job, when PS runs it would miss to notify me about the failure?

Thanks
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: execute PowerShell script only on failures

Post by Kazz »

Is there a typo in this line ?
$JobID = $ParentProc.CommandLine.Split()[7].Trim(' " ')
should it be $JobID = $ParentProc.CommandLine.Split()[7].Trim("")
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: execute PowerShell script only on failures

Post by Kazz »

I am getting at error in $JobID = $ParentProc.CommandLine.Split()[7].Trim('"')

$ParentProc.CommandLine returns the following

CommandLine : "C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.Manager.exe"
"startbackupjob" "owner=[vbsvc]" "Normal" "1ea4e96f-13f7-4e9b-b73d-968f75a4c425"
"2f1b259b-1d80-40f2-bd09-39e662c35037"
david.domask
Veeam Software
Posts: 2566
Liked: 603 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: execute PowerShell script only on failures

Post by david.domask »

Hi @Kazz,

1. Okay, your idea is in my opinion good use of post-job scripts, so I think you're on the right track.
2. For your error, it might just be copy/paste errors as the text gets passed through forums/to your machine. Let me check this again in lab, but last I tested on VBR v12 CP3, it worked as I wrote it :)

But checking in my lab, it works just fine with:

Code: Select all

Import-Module Veeam.Backup.Powershell 
$ScriptPID = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $PID}
$ParentProc = Get-WMIObject win32_process | Where-Object {$_.ProcessID -eq $ScriptPID.ParentProcessId}
$JobID = $ParentProc.CommandLine.Split()[7].Trim('"')
$Job = Get-VBRJob | Where-Object {$_.id -eq $JobID}
What's your error?
David Domask | Product Management: Principal Analyst
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: execute PowerShell script only on failures

Post by Kazz »

David,

There was no typo in your code. I needed to add a 45 sec delay before executing your code, seems like a necessary workaround mentioned in other posts as job status isn't ready when script is executed, so start-sleep 45 did the trick for me.
david.domask
Veeam Software
Posts: 2566
Liked: 603 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: execute PowerShell script only on failures

Post by david.domask »

@Kazz, ah great to hear. The sleep is still a surprise for me since at least in lab it doesn't cause issues, but perhaps it's just that my lab isn't "stressed" enough to hit it. Regardless, glad it worked and I do really like the idea.

If you don't mind, can you share what the connector code to Teams looks like? (Without any secrets added ofc), just not sure I have seen how that works.
David Domask | Product Management: Principal Analyst
Kazz
Expert
Posts: 163
Liked: 18 times
Joined: Feb 08, 2018 3:47 am
Full Name: Kazz Beck
Contact:

Re: execute PowerShell script only on failures

Post by Kazz » 1 person likes this post

I was gonna write something myself, but looks like I don't have the reinvent the wheel.
There are two options for teams notification published in the community.

I don't even need the code you shared, because VeeamNotify has an option to send Teams notifications on Failures only.

https://github.com/realslacker/VeeamTeamsNotifications

https://github.com/tigattack/VeeamNotify
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests