PowerShell script exchange
Post Reply
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

How to get job name in post job command

Post by dcd62 »

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)
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

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.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

I've split and renamed the topic in order not to discuss different issues within one thread. Thanks.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

Thanks, I'll take a look at it.
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

Kindly, let us know how well it goes. Thanks.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

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
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

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.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

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
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

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.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

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
tsightler
VP, Product Management
Posts: 6035
Liked: 2860 times
Joined: Jun 05, 2009 12:57 pm
Full Name: Tom Sightler
Contact:

Re: How to get job name in post job command

Post by tsightler » 3 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.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

Hi, I recently upgraded from v8 to v9.5. I use this part of my script to perform a failover and permanent failover in one command.

This worked well in v8 but when running it on v9.5, I get an error:
9/7/2017 10:30:33 AM Error Permanent failover failed at 9/7/2017 10:30:33 AM. Error: Replica VM VTSVM0001_veeam is not in failover state.

Get-VBRRestorePoint -Name $VM | Sort-Object {$_.creationtime} -Descending | Select -First 1 | Start-VBRViReplicaFailover -Reason "Configuration recovery" -RunAsync -Definite -Confirm: $false

If I run the command first without "-Definite" it will perform the failover. Then if I run it again with "-Definite" it will perform the permanent failover.

Is there a way to combine them in one command. Or should I the first command, add a wait statement, then run the second command with "-Definite"
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

Correct, these days it needs to be a two step process (similar to GUI experience):

- Failover
- Permanent Failover

Thanks.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: How to get job name in post job command

Post by dcd62 »

Excellent. thanks for your quick response
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: How to get job name in post job command

Post by veremin »

You're welcome. Let us know, should you face any other problems.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests