PowerShell script exchange
Post Reply
dfolks
Lurker
Posts: 1
Liked: never
Joined: Jan 17, 2020 2:47 pm
Full Name: Dave Folksman
Contact:

Powershell cmd-let to get name of job that invoked script.

Post by dfolks » Jan 17, 2020 2:50 pm

Hi all,

I'm looking for a powershell cmd-let that lets me get the name of the job that invoked the post job script, so I can store it in a variable.

Im trying to run manual truncation on our exchange servers using a script, but i want to pass the name of the job to the script to save me having to write custom scripts for each job.

Thanks.

tsightler
VP, Product Management
Posts: 5537
Liked: 2356 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: Powershell cmd-let to get name of job that invoked script.

Post by tsightler » Jan 17, 2020 4:53 pm 1 person likes this post

So I wrote this little piece of magic back a few years ago (OK, a lot of years ago):

Code: Select all

# Determine job name from calling Veeam.Backup.Manager process
$parentPid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
$parentCmd = (Get-WmiObject Win32_Process -Filter "processid='$parentPid'").CommandLine
$cmdArgs = $parentCmd.Replace('" "','","').Replace('"','').Split(',')
$jobName = (Get-VBRJob | ?{$cmdArgs[4] -eq $_.Id.ToString()}).Name
Veeam doesn't pass the job name as a parameter, so the code above instead finds the parent process that called the script, which, for pre/post-job scripts is the Veeam.Backup.Manager.exe process. The command line for this process includes the JobID, so you can then just grab the job with Get-VBRJob and filtering on the ID.

This was probably first written for v7 (perhaps even 6.5), but as far as I know it still works with 9.5u4 without any changes, at least, I know some customers that are still using scripts I wrote from back then and they haven't complained to me, but I'll admit I haven't tested it in a while. It's a little difficult to troubleshoot since it obviously only works when run as a pre/post-job script.

Usually, in my scripts, I support a $jobName parameter, which you can pass manually from the cmdline for testing, and, only if this is undefined, does it activate the "automatic" job name. This way I can still easily test my scripts from the cmdline while not having to pass individual parameters for every single pre/post-job.

Let me know if you have any issues.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests