PowerShell script exchange
Post Reply
Aravind
Novice
Posts: 6
Liked: never
Joined: Mar 16, 2023 6:51 am
Full Name: Aravind
Contact:

Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replication

Post by Aravind »

Hello,

Currently we are working on powershell script which pushes the backup related information to a third party tool. We want to know is there any way to pass the arguments like Backup job name or job id in the script configuration.
Suppose lets say a backup job called "Agent back up" starts and get finished. After this the powershell script gets executed where we need to pass the name of backup job (in this case it is "Agent back up"). So like from where should we need get this name and pass it in the script?

Hoping to get some help in this forum.
david.domask
Veeam Software
Posts: 2069
Liked: 503 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by david.domask »

Hi @Aravind, do you want just general collection on the jobs running or you want each job to report on itself when it runs?

I think probably you want something like MyVeeamReport: https://gist.github.com/smasterson/9136468

You could use the code from there as a framework to understand what cmdlets to use to get more information.

If you want to have each job report on itself, you can use this code to get the job from a pre/post job script:

$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}

The $job variable will return the job itself and you can pass the Job to the various session cmdlets: https://helpcenter.veeam.com/docs/backu ... ml?ver=120
David Domask | Product Management: Principal Analyst
marco.horstmann
Veeam Software
Posts: 611
Liked: 110 times
Joined: Dec 31, 2014 3:05 pm
Full Name: Marco Horstmann
Location: Hannover, Germany
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by marco.horstmann » 1 person likes this post

Hi Aravind,

I think I implemented all you need in this examples:

Code: Select all

function Get-MHOVbrJobNameFromPID {
    $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
    return $jobName
}
https://github.com/marcohorstmann/psscr ... veeam.psm1
https://horstmann.in/log-your-errors-di ... veeam-job/

Regards
Marco
Marco Horstmann
Senior System Engineer @ Veeam Software

@marcohorstmann
https://horstmann.in
VMware VCP
NetApp NCIE-SAN for 7-Mode and Clustered Ontap
Aravind
Novice
Posts: 6
Liked: never
Joined: Mar 16, 2023 6:51 am
Full Name: Aravind
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by Aravind »

@ marco.horstmann
$jobName = (Get-VBRJob | ? {$cmdArgs[4] -eq $_.Id.ToString()}).Name
This command is returning null
I am getting $cmdArgs as - D:\Veeam Backup\Console\veeam.backup.shell.exe
$cmdArgs[4] is empty in my case.
What to do?
Aravind
Novice
Posts: 6
Liked: never
Joined: Mar 16, 2023 6:51 am
Full Name: Aravind
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by Aravind »

david.domask wrote: Mar 16, 2023 9:30 am Hi @Aravind, do you want just general collection on the jobs running or you want each job to report on itself when it runs?

I think probably you want something like MyVeeamReport: https://gist.github.com/smasterson/9136468

You could use the code from there as a framework to understand what cmdlets to use to get more information.

If you want to have each job report on itself, you can use this code to get the job from a pre/post job script:

$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}

The $job variable will return the job itself and you can pass the Job to the various session cmdlets: https://helpcenter.veeam.com/docs/backu ... ml?ver=120
$JobID = $ParentProc.CommandLine.Split()[7].Trim('"')
This command is not working in my case.
I am getting $ParentProc as "D:\Veeam Backup\Console\veeam.backup.shell.exe"
Aravind
Novice
Posts: 6
Liked: never
Joined: Mar 16, 2023 6:51 am
Full Name: Aravind
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by Aravind »

marco.horstmann wrote: Mar 20, 2023 9:03 am Hi Aravind,

I think I implemented all you need in this examples:

Code: Select all

function Get-MHOVbrJobNameFromPID {
    $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
    return $jobName
}
https://github.com/marcohorstmann/psscr ... veeam.psm1
https://horstmann.in/log-your-errors-di ... veeam-job/

Regards
Marco
$jobName = (Get-VBRJob | ? {$cmdArgs[4] -eq $_.Id.ToString()}).Name
This command is returning null
I am getting $cmdArgs as - "D:\Veeam Backup\Console\veeam.backup.shell.exe"
$cmdArgs[4] is empty in my case.
What to do?
david.domask
Veeam Software
Posts: 2069
Liked: 503 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by david.domask »

Hi @Aravind,

I just tested on fully patched v12, and for me the code worked :)

Code: Select all

3439443e-b148-4e7d-9a33-3f2bb1fa0437

Job Name                  Type            State      Last Result  Description                                          
--------                  ----            -----      -----------  -----------                                          
vmware-ffi-cap-bb         VMware Backup   Working    None         Created by DDOM-VEEAM-RB4\some-guy at 8/29/2022 11:14...
The above got printed to a file I Out-Filed the last variable to.

What type of job did you try it with? I've not tested this with Agent based jobs, so maybe that's the difference? And not sure on Backup Copies, since with the new Backup Copy format, that gets a bit tricky, but maybe you can output the $Job variable to some file in your pre-job script and show what it returns.
David Domask | Product Management: Principal Analyst
marco.horstmann
Veeam Software
Posts: 611
Liked: 110 times
Joined: Dec 31, 2014 3:05 pm
Full Name: Marco Horstmann
Location: Hannover, Germany
Contact:

Re: Passing the variable/ argument like backup Job name or job id, in script configuration in veeam backup and replicati

Post by marco.horstmann »

@Aravind Did you run this manually? Because it needs to be run as Pre-Job Script. Otherwise it would not work.
Marco Horstmann
Senior System Engineer @ Veeam Software

@marcohorstmann
https://horstmann.in
VMware VCP
NetApp NCIE-SAN for 7-Mode and Clustered Ontap
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests