1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ coverage/
.*.sw[op]
.DS_Store
.rspec
tmp/

17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
##2015-05-24 - Supported Release 2.0.1
##2016-07-12 - Supported Release 2.0.2
###Summary

Small release with bugs fixes and another speed improvement.

###Features
- Noticable speed increase by reducing the time start a PowerShell command ([MODULES-3406](https://tickets.puppetlabs.com/browse/MODULES-3406))

###Bug Fixes
- Fixed minor bugs in tests ([MODULES-3347](https://tickets.puppetlabs.com/browse/MODULES-3347))
- Added tests for try/catch ([MODULES-2634](https://tickets.puppetlabs.com/browse/MODULES-2634))
- Fixed bug with older ruby (1.8)

##2016-05-24 - Supported Release 2.0.1
###Bug Fixes

- Updated the powershell manager in this module in order to not conflict with the Powershell Manager in the Puppet DSC module

##2015-05-17 - Supported Release 2.0.0
##2016-05-17 - Supported Release 2.0.0
###Summary

Major release with performance improvements
Expand Down
36 changes: 23 additions & 13 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ if explicitly_require_windows_gems
# This also means Puppet Gem less than 3.5.0 - this has been tested back
# to 3.0.0. Any further back is likely not supported.
if puppet_gem_location == :gem
gem "ffi", "1.9.0", :require => false
gem "win32-eventlog", "0.5.3", :require => false
gem "win32-process", "0.6.5", :require => false
gem "win32-security", "~> 0.1.2", :require => false
gem "win32-service", "0.7.2", :require => false
gem "minitar", "0.5.4", :require => false
gem "ffi", "1.9.0", :require => false
gem "win32-eventlog", "0.5.3","<= 0.6.5", :require => false
gem "win32-process", "0.6.5","<= 0.7.5", :require => false
gem "win32-security", "~> 0.1.2","<= 0.2.5", :require => false
gem "win32-service", "0.7.2","<= 0.8.7", :require => false
gem "minitar", "0.5.4", :require => false
else
gem "ffi", "~> 1.9.0", :require => false
gem "win32-eventlog", "~> 0.5", :require => false
gem "win32-process", "~> 0.6", :require => false
gem "win32-security", "~> 0.1", :require => false
gem "win32-service", "~> 0.7", :require => false
gem "minitar", "~> 0.5.4", :require => false
gem "ffi", "~> 1.9.0", :require => false
gem "win32-eventlog", "~> 0.5","<= 0.6.5", :require => false
gem "win32-process", "~> 0.6","<= 0.7.5", :require => false
gem "win32-security", "~> 0.1","<= 0.2.5", :require => false
gem "win32-service", "~> 0.7","<= 0.8.7", :require => false
gem "minitar", "~> 0.5.4", :require => false
end

gem "win32-dir", "~> 0.3", :require => false
gem "win32-dir", "~> 0.3","<= 0.4.9", :require => false
gem "win32console", "1.3.2", :require => false if RUBY_VERSION =~ /^1\./

# Puppet less than 3.7.0 requires these.
Expand All @@ -124,6 +124,16 @@ if explicitly_require_windows_gems
gem "win32-taskscheduler", "0.2.2", :require => false
gem "windows-api", "0.4.3", :require => false
gem "windows-pr", "1.2.3", :require => false
else
if Gem::Platform.local.os == 'mingw32'
# If we're using a Puppet gem on windows, which handles its own win32-xxx gem dependencies (Pup 3.5.0 and above), set maximum versions
# Required due to PUP-6445
gem "win32-dir", "<= 0.4.9", :require => false
gem "win32-eventlog", "<= 0.6.5", :require => false
gem "win32-process", "<= 0.7.5", :require => false
gem "win32-security", "<= 0.2.5", :require => false
gem "win32-service", "<= 0.8.7", :require => false
end
end

# Evaluate Gemfile.local if it exists
Expand Down
58 changes: 30 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#powershell
# powershell

####Table of Contents
#### Table of Contents

1. [Overview](#overview)
2. [Module Description - What the module does and why it is useful](#module-description)
Expand All @@ -12,20 +12,20 @@
5. [Limitations - OS compatibility, etc.](#limitations)
6. [Development - Guide for contributing to the module](#development)

##Overview
## Overview

This module adds a new exec provider capable of executing PowerShell commands.

##Module Description
## Module Description

Puppet provides a built-in `exec` type that is capable of executing commands. This module adds a `powershell` provider to the `exec` type, which enables `exec` parameters, listed below. This module is particularly helpful if you need to run PowerShell commands but don't know the details about how PowerShell is executed, since you can technically run PowerShell commands in Puppet without the module.

##Setup
## Setup

###Setup Requirements
### Setup Requirements
This module requires PowerShell to be installed and the `powershell.exe` to be available in the system PATH.

###Beginning with powershell
### Beginning with powershell

The powershell module adapts the Puppet [exec](http://docs.puppet.com/references/stable/type.html#exec) resource to run PowerShell commands. To get started, simply install the module and declare 'powershell' in `provider` with the applicable command.

Expand All @@ -36,7 +36,7 @@ exec { 'RESOURCENAME':
}
~~~

##Usage
## Usage

When using `exec` resources with the `powershell` provider, the `command` parameter must be single-quoted to prevent Puppet from interpolating `$(..)`.

Expand Down Expand Up @@ -77,60 +77,62 @@ $obj.Rename("OtherGuest")
This has the added benefit of not requiring escaping '$' in the PowerShell code. Note that the files need to have DOS linefeeds or they will not work as expected. One tool for converting UNIX linefeeds to DOS linefeeds is [unix2dos](http://freecode.com/projects/dos2unix).


##Reference
## Reference

####Provider
#### Provider
* powershell - Adapts the Puppet `exec` resource to run PowerShell commands.

####Parameters
#### Parameters
All parameters are optional.

#####`creates`
##### `creates`
Specifies the file to look for before running the command. The command will only run if the file doesn't exist. **Note: This parameter will not create a file, it will simpy look for one.** Valid options: A string of the path to the file. Default: Undefined.

#####`cwd`
##### `cwd`
Sets the directory from which to run the command. Valid options: A string of the directory path. Default: Undefined.

#####`command`
##### `command`
Specifies the actual PowerShell command to execute. Must either be fully qualified or a search path for the command must be provided. Valid options: String. Default: Undefined.

#####`environment`
##### `environment`
Sets additional environment variables to set for a command. Valid options: String, or an array of multiple options. Default: Undefined.

#####`logoutput`
##### `logoutput`
Defines whether to log command output in addition to logging the exit code. If you specify 'on_failure', it only logs the output when the command has an exit code that does not match any value specified by the `returns` attribute. Valid options: 'true', 'false', and 'on_failure'. Default: 'on_failure'.

#####`onlyif`
##### `onlyif`
Runs the exec only if the command returns 0. Valid options: String. Default: Undefined.

#####`path`
##### `path`
Specifies the search path used for command execution. Valid options: String of the path, an array, or a semicolon-separated list. Default: Undefined.

#####`refresh`
##### `refresh`
Refreshes the command. Valid options: String. Default: Undefined.

#####`refreshonly`
##### `refreshonly`
Refreshes the command only when a dependent object is changed. Used with `subscribe` and `notify` [metaparameters](http://docs.puppet.com/references/latest/metaparameter.html). Valid options: 'true', 'false'. Default: 'false'.

#####`returns`
##### `returns`
Lists the expected return code(s). An error will be returned if the executed command returns something else. Valid options: An array of acceptable return codes or a single value. Default: 0.

#####`timeout`
##### `timeout`
Sets the maximum time in seconds that the command should take. Valid options: Number or string representation of a number. Default: 300.

#####`tries`
##### `tries`
Determines the number of times execution of the command should be attempted. Valid options: Number or a string representation of a number. Default: '1'.

#####`try_sleep`
##### `try_sleep`
Specifies the time to sleep in seconds between `tries`. Valid options: Number or a string representation of a number. Default: Undefined.

#####`unless`
##### `unless`
Runs the `exec`, unless the command returns 0. Valid options: String. Default: Undefined.

##Limitations
## Limitations

* Only supported on Windows Server 2003 and above, and Windows 7 and above.
* Only supported on Windows Server 2008 and above, and Windows 7 and above.

##Development
* Only supported on Powershell 2.0 and above.

## Development

Puppet Inc modules on the Puppet Forge are open projects, and community contributions are essential for keeping them great. We can’t access the huge number of platforms and myriad hardware, software, and deployment configurations that Puppet is intended to serve. We want to keep it as easy as possible to contribute changes so that our modules work in your environment. There are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things. For more information, see our [module contribution guide.](https://docs.puppet.com/forge/contributing.html)
5 changes: 3 additions & 2 deletions lib/puppet/provider/exec/powershell.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'puppet/provider/exec'
require_relative '../../../puppet_x/puppetlabs/powershell/powershell_manager'
require File.join(File.dirname(__FILE__), '../../../puppet_x/puppetlabs/powershell/powershell_manager')

Puppet::Type.type(:exec).provide :powershell, :parent => Puppet::Provider::Exec do
confine :operatingsystem => :windows
Expand Down Expand Up @@ -98,7 +98,8 @@ def validatecmd(command)
private
def write_script(content, &block)
Tempfile.open(['puppet-powershell', '.ps1']) do |file|
file.write(content)
file.puts(content)
file.puts()
file.flush
yield native_path(file.path)
end
Expand Down
29 changes: 27 additions & 2 deletions lib/puppet_x/puppetlabs/powershell/powershell_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def initialize(cmd)

Puppet.debug "#{Time.now} #{cmd} is running as pid: #{@ps_process[:pid]}"

init_ready_event_name = "Global\\#{SecureRandom.uuid}"
init_ready_event = self.class.create_event(init_ready_event_name)

code = make_ps_init_code(init_ready_event_name)
out, err = exec_read_result(code, init_ready_event)

Puppet.debug "#{Time.now} PowerShell initialization complete for pid: #{@ps_process[:pid]}"

at_exit { exit }
end

Expand Down Expand Up @@ -90,12 +98,29 @@ def template_path
File.expand_path('../../../templates', __FILE__)
end

def make_ps_code(powershell_code, output_ready_event_name, timeout_ms = 300 * 1000)
template_file = File.new(template_path + "/invoke_ps_command.erb").read
def make_ps_init_code(init_ready_event_name)
template_file = File.new(template_path + "/init_ps.ps1.erb").read
template = ERB.new(template_file, nil, '-')
template.result(binding)
end

def make_ps_code(powershell_code, output_ready_event_name, timeout_ms = 300 * 1000)
<<-CODE
$params = @{
Code = @'
#{powershell_code}
'@
EventName = "#{output_ready_event_name}"
TimeoutMilliseconds = #{timeout_ms}
}
Invoke-PowerShellUserCode @params
# always need a trailing newline to ensure PowerShell parses code
CODE
end

private

def self.is_readable?(stream, timeout = 0.5)
Expand Down
Loading