Scripting the - Run script before and after the job

PowerShell script exchange

Scripting the - Run script before and after the job

Veeam Logoby dcd62 » Thu Apr 09, 2015 7:21 pm

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

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

Veeam Logoby v.Eremin » Fri Apr 10, 2015 9:38 am

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.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

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

Veeam Logoby dcd62 » Wed May 20, 2015 5:08 pm

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

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

Veeam Logoby v.Eremin » Thu May 21, 2015 8:57 am

What product version you're using? What is PS version?
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

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

Veeam Logoby dcd62 » Tue May 26, 2015 5:00 pm

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

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

Veeam Logoby dcd62 » Wed May 27, 2015 5:10 pm 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
dcd62
Enthusiast
 
Posts: 29
Liked: 4 times
Joined: Wed Aug 20, 2014 1:00 pm

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

Veeam Logoby v.Eremin » Fri May 29, 2015 9:59 am

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.
v.Eremin
Veeam Software
 
Posts: 13290
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 9 guests