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

Scripting the - Run script before and after the job

Post by dcd62 »

I have a Powershell script to create my replication jobs and want to run another powershell script before and after the job is run. I can manually edit the jobs and select the option and provide the path the scripts.

I would rather add the commands to select the run before and after scripts (and provide the path) to my initial script instead of manually editing each job . Is that a possible option to script in Powershell? I could not find any examples.
veremin
Product Manager
Posts: 20405
Liked: 2298 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scripting the - Run script before and after the job

Post by veremin »

You can modify the post job settings of given job, using this code:

Code: Select all

$Job = Get-VBRJob -name "Job Name"
$Options = $Job.GetOptions()
$Options.Options.RootNode.PostJobCommand.Enabled = "True"
$Options.Options.RootNode.PostJobCommand.CommandLine = "Your command"
$Job.SetOptions($Options)
Thanks.
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: Scripting the - Run script before and after the job

Post by dcd62 »

Hi, I'm just getting to test this out.
I added the script below to my job creation script and it gave an error when trying to create the post job option. The errors were on the "enabled" and "command line" property

Code: Select all

[b]Script[/b]
   	#Set Pre Job Options to convert templates to virtual machines
   	$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\TemplateRep2VM.ps1"
	$Job.SetOptions($Options)

Code: Select all

[b]ERROR:[/b]
The property 'Enabled' cannot be found on this object. Verify that the property exists and can be set.
At D:\Scripts\Templates\Working\BuildTemplateCreate_vmrep_Job.ps1:371 char:2
+     $Options.Options.RootNode.PostJobCommand.Enabled = "True"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

The property 'CommandLine' cannot be found on this object. Verify that the property exists and can be set.
At D:\Scripts\Templates\Working\BuildTemplateCreate_vmrep_Job.ps1:372 char:2
+     $Options.Options.RootNode.PostJobCommand.CommandLine = "C:\Windows\SysWOW64\Win ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
veremin
Product Manager
Posts: 20405
Liked: 2298 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scripting the - Run script before and after the job

Post by veremin »

What product version you're using? What is PS version?
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: Scripting the - Run script before and after the job

Post by dcd62 »

Veeam Backup and Recovery v8 and PowerShell v4.0
dcd62
Enthusiast
Posts: 39
Liked: 4 times
Joined: Aug 20, 2014 1:00 pm
Contact:

Re: Scripting the - Run script before and after the job

Post by dcd62 » 1 person likes this post

I was able to get it working with the following:

$Options.JobScriptCommand.PostScriptEnabled = "true"
$Options.JobScriptCommand.PostScriptCommandLine = "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe "my command"

thanks
veremin
Product Manager
Posts: 20405
Liked: 2298 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Scripting the - Run script before and after the job

Post by veremin »

As far as I know, post job command can be configured in several ways: either through .rootnode or through .jobscriptcommand. Not sure why the former approach didn't work for you.

But I'm glad to hear that you've nailed it.

Thanks.
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot] and 13 guests