Using Start-Job from a Puppet module

13 views
Skip to first unread message

Emily Gribble

unread,
Apr 16, 2018, 11:43:21 AM4/16/18
to Puppet Users

I am trying to use a puppet module to kick off a powershell script in the background. Start-Job appears to be the best choice for this, but the combination of puppet and Start-Job is not producing the results I would expect. Here are some examples of different options I have tried (all three work from the powershell command line on the node in question). All of these execs return "executed successfully," but fail to actually run the script. Any ideas?


exec { 'foo':
  command => "Start-Job C:/Windows/foo.ps1",
  provider => "powershell"
}


exec { 'foo':
  command => "Start-Job -ScriptBlock {C:/Windows/foo.ps1}",
  provider => "powershell"
}



exec { 'foo':
  command => "Start-Job -File C:/Windows/foo.ps1",
  provider => "powershell"
}

James Pogran

unread,
Apr 16, 2018, 1:04:43 PM4/16/18
to puppet...@googlegroups.com
Hi Emily,

PowerShell Background Jobs created on the local computer are tied to the console that created them, in that two separate powershell.exe windows cannot see the each other's jobs. Since they are 'background', control is returned immediately to the caller when they are created. In your examples below, Start-Job creates the job and completes successfully. The Job starts running and doing whatever its doing. PowerShell sees this as the last command to run and exits. When PowerShell exists, any running process or threads are aborted, and thus your Job as well. Puppet sees a success exit code from PowerShell and closes the run. You could use Wait-Job to wait for the job to finish, but that would block execution until that job finished, which is contrary to the goal I assume you have - running a PowerShell script concurrently while other parts of a Puppet manifest execute.

I don't know of a way to make this work for you for Start-Job. If there is a need to run something while other things run, that starts to enter the arena of the Puppet Orchestrator. I might be able to help with more of an idea of what you are trying to accomplish.



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/81ddfefb-ea11-4cc5-bd08-05a62db0fa5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
James Pogran
Senior Software Engineer, Windows
Puppet. The shortest path to better software

Emily Gribble

unread,
Apr 16, 2018, 5:20:02 PM4/16/18
to Puppet Users
Hi James,

Wait-Job will actually work for my purposes. Thanks for the explanation of what's going on behind the scenes, it definitely helped my understanding of this.


On Monday, April 16, 2018 at 10:04:43 AM UTC-7, James Pogran wrote:
Hi Emily,

PowerShell Background Jobs created on the local computer are tied to the console that created them, in that two separate powershell.exe windows cannot see the each other's jobs. Since they are 'background', control is returned immediately to the caller when they are created. In your examples below, Start-Job creates the job and completes successfully. The Job starts running and doing whatever its doing. PowerShell sees this as the last command to run and exits. When PowerShell exists, any running process or threads are aborted, and thus your Job as well. Puppet sees a success exit code from PowerShell and closes the run. You could use Wait-Job to wait for the job to finish, but that would block execution until that job finished, which is contrary to the goal I assume you have - running a PowerShell script concurrently while other parts of a Puppet manifest execute.

I don't know of a way to make this work for you for Start-Job. If there is a need to run something while other things run, that starts to enter the arena of the Puppet Orchestrator. I might be able to help with more of an idea of what you are trying to accomplish.
On Mon, Apr 16, 2018 at 11:35 AM, Emily Gribble <emilyg...@gmail.com> wrote:

I am trying to use a puppet module to kick off a powershell script in the background. Start-Job appears to be the best choice for this, but the combination of puppet and Start-Job is not producing the results I would expect. Here are some examples of different options I have tried (all three work from the powershell command line on the node in question). All of these execs return "executed successfully," but fail to actually run the script. Any ideas?


exec { 'foo':
  command => "Start-Job C:/Windows/foo.ps1",
  provider => "powershell"
}


exec { 'foo':
  command => "Start-Job -ScriptBlock {C:/Windows/foo.ps1}",
  provider => "powershell"
}



exec { 'foo':
  command => "Start-Job -File C:/Windows/foo.ps1",
  provider => "powershell"
}

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages