How to get job name in post job command

PowerShell script exchange

How to get job name in post job command

Veeam Logoby dcd62 » Wed Apr 15, 2015 4:30 pm

Ok here's my next question.. I'm trying to figure this out but struggling..
I have a script to create one job with 4 vm's. All of the info is imported via an csv file.
Once I perform the replication, I want to immediately perform the permanent failover.
I didn't see any option to set that in the job, so I am trying to execute a post script to perform the failover after the replication runs.
The post script is below..The issue I have is how do I define each vm "$vm" (just put that in for now) to execute the script and start the failover.
Is there a way to get the current job name that is running within that job?

Get-VBRRestorePoint -Name $VM | Sort-Object $_.creationtime -Descending | Select -First 1 | Start-VBRViReplicaFailover -Reason "Committing template" -RunAsync -Definite -Confirm: $true


In my job creation script i have this
#Setting Post Job Script
$Job = Get-VBRJob -name $CreateJobName
$Options = $Job.GetOptions()
$Options.Options.RootNode.PostJobCommand.Enabled = "True"
$Options.Options.RootNode.PostJobCommand.CommandLine = C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe "D:\Scripts\Templates\Working\PermFailoverWinTmpl.ps1"
$Job.SetOptions($Options)
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

Re: How to get job name in post job command

Veeam Logoby v.Eremin » Thu Apr 16, 2015 2:16 pm

I wouldn't ascribe to Tom's work to myself. Instead, I would quote him as is:

tsightler wrote:OK, good to know I wasn't just overlooking something. I've come up with my own hack for this, posted here just in case anyone else has a need for something similar:
Code: Select all
$parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
$parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
$job = Get-VBRJob | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
$session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}

This looks up the parent process ID of the Powershell session, which in the case of pre/post-scripts is the Veeam.Backup.Manager.exe that controls the job session. The script then grabs the command line used to start the manager process process which includes the Veeam job and session UUID and typically looks something like this:
Code: Select all
"C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.Manager.exe" "startbackupjob" "owner=[vbsvc]" "Normal" "a6782dae-5ebe-4aab-9cb3-3a75827dff22" "a432e43e-b17c-4778-b9d2-e8d6519526ea"

It then does a crude match on these UUIDs to find the actual job and session PS objects and I'm good to go with all of the information I need even with nothing passed in. I probably should improve the match code as right now it could potentially get the wrong job if somehow a session ID was exactly the same as a job ID or vice-versa. This seems pretty much impossible to occur in the real world, but I should probably account for it just to be thorough.


Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to get job name in post job command

Veeam Logoby v.Eremin » Fri Apr 17, 2015 4:04 pm

I've split and renamed the topic in order not to discuss different issues within one thread. Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to get job name in post job command

Veeam Logoby dcd62 » Fri Apr 17, 2015 5:38 pm

Thanks, I'll take a look at it.
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

Re: How to get job name in post job command

Veeam Logoby v.Eremin » Tue Apr 21, 2015 9:18 am

Kindly, let us know how well it goes. Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to get job name in post job command

Veeam Logoby dcd62 » Thu May 28, 2015 6:52 pm

Hi, not really sure if I did this right, but it didn't work.
I created a job and let the initial replication complete. Then I added a post script job with the lines you provided and to start the failover.. I don't know how to check to why it did not failover.

Code: Select all
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe "D:\Scripts\Templates\Working\PermFailoverWinTmpl.ps1"

$parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
   $parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
   $job = Get-VBRJob | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
   $session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}
   
   Get-VBRRestorePoint -Name $session | Sort-Object $_.creationtime -Descending | Select -First 1 | Start-VBRViReplicaFailover -Reason "Permanent failover" -RunAsync -Definite -Confirm: $true
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

Re: How to get job name in post job command

Veeam Logoby v.Eremin » Fri May 29, 2015 10:14 am

Try to input $session variable into console and see what information it contains.

I don't have a console at hand at the moment, but based on the Tom's post I seriously doubt that it contains something like Restore Point name.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to get job name in post job command

Veeam Logoby dcd62 » Fri May 29, 2015 3:39 pm

It looks like it grabs the PID but $job and $session do not get populated

PS D:\Scripts\Veeam\Templates> $parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
PS D:\Scripts\Veeam\Templates> $parentpid
7164

PS D:\Scripts\Veeam\Templates> $parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
PS D:\Scripts\Veeam\Templates> $parentcmd
"C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.Shell.exe"

PS D:\Scripts\Veeam\Templates> $job = Get-VBRJob | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
PS D:\Scripts\Veeam\Templates>

PS D:\Scripts\Veeam\Templates> $session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}
PS D:\Scripts\Veeam\Templates> $session
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

Re: How to get job name in post job command

Veeam Logoby v.Eremin » Fri May 29, 2015 4:44 pm

I'm not even sure why you're even trying to grab job name, when in reality the failover in Powershell is executed against particular restore point, not a job as whole.

I assume that finding the latest successful restore point for particular VMs in post-activity and running failover for them might be a better idea.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13281
Liked: 970 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: How to get job name in post job command

Veeam Logoby dcd62 » Fri May 29, 2015 6:58 pm

Hi, so the reason I'm trying to do this is that we are creating an HPOO flow to handle scheduling the replications and performing the final sync/failover. We use veeam to move vm's over to other data centers. I'm trying to reduce the number of steps to perform the process. Right now I have individual flows for each step. My goal is to script a final sync then immediately perform the failover. When I add two steps into one script it doesn't work because it will start the final replication then immediately try to failover. So my thought is that the job would be created and the initial rep perform. Then later when it's time to cut over, we kick off another script to start a final syc and add a post job that would immediately fail it over.

Maybe I'm approaching this the wrong way or missing something but I'm open to any ideas..
Thanks!

I have a script to perform a final sync:
#Enter VM name to start job
$VM = $args[0]
#Starts the final replication process
Get-VBRJob -Name $VM
Start-VBRJob -Job $VM -RunAsync

Then I have another script to perform the permanent failover:
Get-VBRRestorePoint -Name $VM | Sort-Object $_.creationtime -Descending | Select -First 1 | Start-VBRViReplicaFailover -Reason "Configuration recovery" -RunAsync -Definite -Confirm: $true
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

Re: How to get job name in post job command

Veeam Logoby tsightler » Fri May 29, 2015 8:38 pm 2 people like this post

dcd62 wrote:It looks like it grabs the PID but $job and $session do not get populated

PS D:\Scripts\Veeam\Templates> $parentpid = (Get-WmiObject Win32_Process -Filter "processid='$pid'").parentprocessid.ToString()
PS D:\Scripts\Veeam\Templates> $parentpid
7164

PS D:\Scripts\Veeam\Templates> $parentcmd = (Get-WmiObject Win32_Process -Filter "processid='$parentpid'").CommandLine
PS D:\Scripts\Veeam\Templates> $parentcmd
"C:\Program Files\Veeam\Backup and Replication\Backup\Veeam.Backup.Shell.exe"

PS D:\Scripts\Veeam\Templates> $job = Get-VBRJob | ?{$parentcmd -like "*"+$_.Id.ToString()+"*"}
PS D:\Scripts\Veeam\Templates>

PS D:\Scripts\Veeam\Templates> $session = Get-VBRBackupSession | ?{($_.OrigJobName -eq $job.Name) -and ($parentcmd -like "*"+$_.Id.ToString()+"*")}
PS D:\Scripts\Veeam\Templates> $session


You can't just run this at any Powershell prompt, the code only works when called from pre/post-job because that's the only scenario when the parent process is the Veeam Manager process. To troubleshoot it you'd need to add some code to write out the variables to a file during execution and run it from a post-job script in a small job. However, I'm using this code at a number of customers and it seems to work reliably so I doubt that's the actual problem.

It sounds like you just want to script sync and permanent failover, which shouldn't really require any post-job activity. Just a loop to monitor progress and move to the next step once the previous step is finished is really all that should be required.
tsightler
Veeam Software
 
Posts: 4769
Liked: 1737 times
Joined: Fri Jun 05, 2009 12:57 pm
Full Name: Tom Sightler


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 4 guests