Ask Your Question
0

How can I use my puppet master to execute a powershell script on a remote windows 2012

asked 2015-09-18 07:02:37 -0500

michaelokoluko gravatar image

updated 2015-09-22 08:09:47 -0500

I have created the init.pp file and whenever I run the puppet parser validation on that file i get the below error:

root@ip-172-31-42-99:/etc/puppetlabs/code/environments/production/modules/downloadScript/manifests# puppet parser validate init.pp Error: Could not parse for environment production: Syntax error at 'command' at /etc/puppetlabs/code/environments/production/modules/downloadScript/manifests/init.pp:4:5
root@ip-172-31-42-99:/etc/puppetlabs/code/environments/production/modules/downloadScript/manifests#

The file seats in the windows hosts running the puppet agent.

Questions

  1. Do I need another .pp file to be created? i.e. site.pp or a class
  2. Why is puppet not picking up this exec command when i run puppet agent -t.
  3. Also I can't see it in my class or resource listings on the console.
  4. Finally how do I use the "unless" or "onlyif" attribute in this context
  5. Do i need a notify attribute?
  6. Any alternate method I can use like DSC (Desired State Configuration)

Below is the code - am i missing something?

exec {'c:\\download.ps1':
    # if you want this to only run under certain conditions, use the 'unless' or onlyif'
    path        => 'C:/WINDOWS/Sytems32/WindowsPowerShell/v1.0'
    command     => 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe - file c:\\download.ps1',
    logoutput   => true,
    refreshonly => true,
    provider    => powershell
  }
}

Also tried the below...

exec {'c:\\download.ps1':
    # if you want this to only run under certain conditions, use the 'unless' or onlyif'
    path     => 'C:/WINDOWS/Sytems32/WindowsPowerShell/v1.0'
    command  => "${C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe} - file c:\\download.ps1",
    logoutput => true,
    refreshonly => true,
    provider => powershell
  }
}

on the back of this:

exec {'ps_remoting_enable.ps1':
   path     => 'C:/WINDOWS/Sytems32/WindowsPowerShell/v1.0',
   command  => '${psh_cmd} - file c:\\ps_remoting_enable.ps1',
   provider =>  powershell,
}

A bit of back ground...

All i need, is for puppet to execute the "download.ps1" on the windows 2012 server - which is basically a script that downloads the latest machine image and apply to the local windows machine when a change or bug has been fixed. At the moment we manually connect to the windows box double click the ps1 script and it just runs and when it finishes it says "press any key to continue" and the powershell cmd prompt dialog box disappears.

I have looked at the exec command puppetlabs docs and the powersell exec example in forge but I'm not having any luck and I'm even considering using Salt (cmd.run), seems less of a hassle than puppet where you have to worry about What .pp file to create where to place it, what code parameters to use - its such a pain compared to salt - its just a simple case of puppet run this script and only run when a new build is available to be downloaded.

Let me know if you need more info.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2015-09-23 12:20:34 -0500

souldonut gravatar image

updated 2015-09-23 12:32:41 -0500

ramindk gravatar image

Ok...taking this piece by piece without changing chunks of the code, you want to clean up the command line

Command line

You have:

command => 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe - file c:\download.ps1',

You'd want:

command => 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File c:\download.ps1',

Refresh Only As stated in the other response, you need to either remove this or have another resource trigger it using Notify.

onlyif/unless You need to validate the script somehow, otherwise it will execute download.ps1 every time Puppet runs.. This would be a query for what the result of download.ps1 contains. So, if you are grabbing a file from a URL to a certain location, running a Test-Path query with an exit status would do the trick. I can't quite remember the way to do that when not using the powershell provider for Exec, but it should be along the lines of:

unless => 'c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe If (Test-Path <file that was downloaded>) { exit 1 }'

That should make it all happy, though I'd recommend looking into two things. One, the Powershell exec provider, here. Two, contemplate what download.ps1 is doing. You may be able to put a chunk of the work into Puppet and avoid having a separate script in the first place, but if you need the script there is some guidance in the powershell module in how to do that a bit cleaner (see the part about templates).

I think DSC would only really help in this case if the puppet module supports remotefile resources, though there are instructions in there to customize it. YMMV.

edit flag offensive delete link more
0

answered 2015-09-22 00:30:39 -0500

csharpsteen gravatar image

The syntax error you are getting, Syntax error at 'command', indicates that there is a problem with how the resource is constructed. The syntax of a Puppet resource requires that attributes , such as path and `command, be separated by commas:

exec {'c:\download.ps1':
  # if you want this to only run under certain conditions, use the 'unless' or onlyif'
  path => 'C:/WINDOWS/Sytems32/WindowsPowerShell/v1.0',
  command => 'C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe - file c:\download.ps1',
  logoutput => true,
  refreshonly => true,
  provider => powershell
}

2.Why is puppet not picking up this exec command when i run puppet agent -t.

3.Also I can't see it in my class or resource listings on the console.

The exec resource will need to be applied using a class or node definition in order to be picked up by puppet agent. The PE Quick Start guide covers how to do this with the console:

http://docs.puppetlabs.com/pe/2015.2/...starthelloworld.html

4.Finally how do I use the "unless" or "onlyif" attribute in this context

The unless and onlyif attributes function the same way for Powershell execs as they do for Linux shell execs: if a command specified by these attributes has an exit code of 0, then the resource will be skipped or synced.

5.Do i need a notify attribute?

Yes. Since the refreshonly attribute was set to true, something will have to send a notification to the exec in order for it to fire.

6.Any alternate method I can use like DSC (Desired State Configuration)

A Puppet module was just released that integrates DSC into agent runs using puppet resources:

https://forge.puppetlabs.com/puppetla...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-18 07:01:38 -0500

Seen: 1,909 times

Last updated: Sep 23 '15