powershell
Version information
This version is compatible with:
- Puppet Enterprise 2023.8.x, 2023.7.x, 2023.6.x, 2023.5.x, 2023.4.x, 2023.3.x, 2023.2.x, 2023.1.x, 2023.0.x, 2021.7.x, 2021.6.x, 2021.5.x, 2021.4.x, 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x
- Puppet >= 7.0.0 < 9.0.0
- , , , , , ,
Start using this module
Add this module to your Puppetfile:
mod 'puppetlabs-powershell', '6.0.0'
Learn more about managing modules with a PuppetfileDocumentation
powershell
Table of Contents
- Overview
- Module Description - What the module does and why it is useful
- Setup - The basics of getting started with powershell
- Usage - Configuration options and additional functionality
- Reference - An under-the-hood peek at what the module is doing and how
- Limitations - OS compatibility, etc.
- Development - Guide for contributing to the module
Overview
This module adds a new exec provider capable of executing PowerShell commands.
Module Description
Puppet provides a built-in exec
type that is capable of executing commands. This module adds a powershell
and pwsh
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 how PowerShell is executed, because you can run PowerShell commands in Puppet without the module.
Setup
Requirements
The powershell
provider requires you install Windows PowerShell and have powershell.exe
available in the system PATH. Note that most Windows operating systems already have Windows PowerShell installed.
The pwsh
provider requires you install PowerShell Core and make pwsh
available either in the system PATH or specified in the path
parameter.
For example, when you install PowerShell Core in /usr/alice/pscore
, you need the following manifest:
exec { 'RESOURCENAME':
...
path => '/usr/alice/pscore',
provider => pwsh,
}
Beginning with powershell
The powershell module adapts the Puppet exec resource to run PowerShell commands. To get started, install the module and declare 'powershell' in provider
with the applicable command.
exec { 'RESOURCENAME':
command => 'SOMECOMMAND',
provider => powershell,
}
Usage
When using exec
resources with the powershell
or pwsh
provider, the command
parameter must be single-quoted to prevent Puppet from interpolating $(..)
.
For instance, to rename the Guest account:
exec { 'rename-guest':
command => '(Get-WMIObject Win32_UserAccount -Filter "Name=\'guest\'").Rename("new-guest")',
unless => 'if (Get-WmiObject Win32_UserAccount -Filter "Name=\'guest\'") { exit 1 }',
provider => powershell,
}
Note that the example uses the unless
parameter to make the resource idempotent. The command
is only executed if the Guest account does not exist, as indicated by unless
returning 0.
Note: PowerShell variables (such as $_
) must be escaped in Puppet manifests either using backslashes or single quotes.
Alternately, you can put the PowerShell code for the command
, onlyif
, and unless
parameters into separate files, and then invoke the file function in the resource. You could also use templates and the template()
function if the PowerShell scripts need access to variables from Puppet.
exec { 'rename-guest':
command => file('guest/rename-guest.ps1'),
onlyif => file('guest/guest-exists.ps1'),
provider => powershell,
logoutput => true,
}
Each file is a PowerShell script that should be in the module's files/
folder.
For example, here is the script at: guest/files/rename-guest.ps1
$obj = $(Get-WMIObject Win32_UserAccount -Filter "Name='Guest'")
$obj.Rename("OtherGuest")
This has the added benefit of not requiring escaping '$' in the PowerShell code. Note that the files must have DOS linefeeds or they will not work as expected. One tool for converting UNIX linefeeds to DOS linefeeds is unix2dos.
External files and exit codes
If you are calling external files, such as other PowerShell scripts or executables, be aware that the last executed script's exitcode is used by Puppet to determine whether the command was successful.
For example, if the file C:\fail.ps1
contains the following PowerShell script:
& cmd /c EXIT 5
& cmd /c EXIT 1
and we use the following Puppet manifest:
exec { 'test':
command => '& C:\fail.ps1',
provider => powershell,
}
Then the exec['test']
resource will always fail, because the last exit code from the external file C:\fail.ps1
is 1
. This behavior might have unintended consequences if you combine multiple external files.
To stop this behavior, ensure that you use explicit Exit
statements in your PowerShell scripts. For example, we changed the Puppet manifest from the above to:
exec { 'test':
command => '& C:\fail.ps1; Exit 0',
provider => powershell,
}
This will always succeed because the Exit 0
statement overrides the exit code from the C:\fail.ps1
script.
Console Error Output
The PowerShell module internally captures output sent to the .NET [System.Console]::Error
stream like:
exec { 'test':
command => '[System.Console]::Error.WriteLine("foo")',
provider => powershell,
}
However, to produce output from a script, use the Write-
prefixed cmdlets such as Write-Output
, Write-Debug
and Write-Error
.
Reference
Provider
-
powershell
: Adapts the Puppetexec
resource to run Windows PowerShell commands. -
pwsh
: Adapts the Puppetexec
resource to run PowerShell Core commands.
Parameters
All parameters are optional.
creates
Specifies the file to look for before running the command. The command runs only if the file doesn't exist. Note: This parameter does not create a file, it only looks for one. Valid options: A string of the path to the file. Default: Undefined.
cwd
Sets the directory from which to run the command. Valid options: A string of the directory path. Default: Undefined.
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
Sets additional environment variables to set for a command. Valid options: String, or an array of multiple options. Default: Undefined.
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
Runs the exec only if the command returns 0. Valid options: String. Default: Undefined.
path
Specifies the search path used for command execution. Valid options: String of the path, an array, or a semicolon-separated list. Default: Undefined.
The pwsh
provider can also use the path to find the pwsh executable.
refresh
Refreshes the command. Valid options: String. Default: Undefined.
refreshonly
Refreshes the command only when a dependent object is changed. Used with subscribe
and notify
metaparameters. Valid options: true, false. Default: false.
returns
Lists the expected return code(s). If the executed command returns something else, an error is returned. Valid options: An array of acceptable return codes or a single value. Default: 0.
timeout
Sets the maximum time in seconds that the command should take. Valid options: Number or string representation of a number. Default: 300. A value of 0
for this property will result in using the default timeout of 300. Inifinite timeout is not supported in this module, but large timeouts are allowed if needed.
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
Specifies the time to sleep in seconds between tries
. Valid options: Number or a string representation of a number. Default: Undefined.
unless
Runs the exec
, unless the command returns 0. Valid options: String. Default: Undefined.
Limitations
-
The
powershell
provider is only supported on:-
Windows Server 2008 and above
-
Windows 7 and above
-
-
The
pwsh
provider is supported on:-
CentOS 7
-
Debian 8.7+, Debian 9
-
Fedora 27, 28
-
MacOS 10.12+
-
Red Hat Enterprise Linux 7
-
Ubuntu 14.04, 16.0.4 and 18.04
-
Windows Desktop 7 and above
-
Windows Server 2008 R2 and above
Note that this module will not install PowerShell on these platforms. For further information see the Linux installation instructions.
-
-
Only supported on Windows PowerShell 2.0 and above, and PowerShell Core 6.1 and above.
-
When using here-strings in inline or templated scripts executed by this module, you must use the double-quote style syntax that begins with
@"
and ends with"@
. The single-quote syntax that begins with@'
and ends with'@
is not supported.Note that any external .ps1 script file loaded or executed with the call operator
&
is not subject to this limitation and can contain any style here-string. For instance, the script file external-code.ps1 can contain any style of here-string:exec { 'external-code': command => '& C:\external-code.ps1', provider => powershell, }
Development
Puppet 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.
Change log
All notable changes to this project will be documented in this file. The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
v6.0.0 (2023-04-24)
Changed
- (CONT-793) - Add Puppet 8/Drop Puppet 6 #400 (jordanbreen28)
v5.2.1 (2023-04-21)
Fixed
- pdksync - (CONT-130) - Dropping Support for Debian 9 #384 (jordanbreen28)
v5.2.0 (2022-10-03)
Added
- pdksync - (GH-cat-11) Certify Support for Ubuntu 22.04 #381 (david22swan)
Fixed
- (MAINT) Dropped support for Windows(7,8,2008 + 2008 R2(Server), Fedora(27+28) and OSX OS's(10.12-.14) #382 (jordanbreen28)
v5.1.0 (2022-06-13)
Added
- pdksync - (GH-cat-12) Add Support for Redhat 9 #379 (david22swan)
- pdksync - (FM-8922) - Add Support for Windows 2022 #370 (david22swan)
- (IAC-1734) - Certify Debian 11 #368 (david22swan)
- pdksync - (IAC-1753) - Add Support for AlmaLinux 8 #364 (david22swan)
- pdksync - (IAC-1751) - Add Support for Rocky 8 #363 (david22swan)
- (IAC-900) - Certify Ubuntu 20.04 #359 (david22swan)
Fixed
- pdksync - (GH-iac-334) Remove Support for Ubuntu 14.04/16.04 #372 (david22swan)
- pdksync - (IAC-1598) - Remove Support for Debian 8 #361 (david22swan)
v5.0.0 (2021-02-27)
Changed
- pdksync - Remove Puppet 5 from testing and bump minimal version to 6.0.0 #330 (carabasdaniel)
v4.1.0 (2020-12-07)
Added
- Add support for Puppet 7 #322 (daianamezdrea)
- (MODULES-10722) Inherit pipe_timeout from timeout #321 (michaeltlombardi)
v4.0.0 (2020-07-09)
Changed
- Correct supported Puppet lower bound to 5.5.0 #282 (michaeltlombardi)
Fixed
- (MODULES-10539) Remove commands idiom from PowerShell provider #287 (michaeltlombardi)
v3.0.1 (2020-01-15)
Added
- (IAC-835) Support added for Debian 10 and CentOS/RHEL 8 #306 (david22swan)
Fixed
- (MODULES-10389) - Safeguard powershell provider loading #277 (michaeltlombardi)
v3.0.0 (2020-01-06)
Changed
- (FM-8475) Replace library code #264 (michaeltlombardi)
Fixed
- (MODULES-9473) Fix Issues Link #259 (RandomNoun7)
- (MODULES-9084) Increase pipe timeout to 180s #257 (michaeltlombardi)
2.3.0
Added
- Metadata for supporting Windows Server 2019 (FM-7693)
- Added a 'pwsh' provider for PowerShell Core (MODULES-8355, MODULES-8356, MODULES-8357, MODULES-8358, MODULES-8359)
- Updated metadata for PowerShell Core support (CentOS, Debian, Fedora, OSX and RedHat) (MODULES-8356)
Changed
- Only initialise constant when not defined (MODULES-7067)
Fixed
- Improved pipe reading in the PowerShell Manager (MODULES-8748)
2.2.0 - 2018-10-29
Added
- Added support for Puppet 6 (MODULES-7833)
Changed
- Updated the module to PDK format (MODULES-7402)
- Updated Beaker to version 4 (MODULES-7658)
2.1.5 - 2018-05-08
Added
- Metadata for supporting Windows Server 2016 (MODULES-4271)
Fixed
- Upgraded message to make .NET Framework requirements clearer when running PowerShell 2.0 (MODULES-7011)
- Fixed timeout handling when the user specifies a timeout parameter value of
0
to substitute the default of 300 seconds (MODULES-7018)
2.1.4 - 2017-03-29
Fixed
- Ensured that the code is able to start the pipes server in a PowerShell process on Windows 2008R2 images (MODULES-6927)
- Updated PowerShell syntax in README examples
2.1.3 - 2017-12-08
Fixed
- Fixed timeouts and zombie process creation (MODULES-4748)
- Corrected PowerShell executable name for experimental cross-platform / PowerShell 6 support (MODULES-6081)
2.1.2 - 2017-07-27
Fixed
- Fixed Global Warning variable (MODULES-5224)
- Moved the PowerShell template file to stop it conflicting with the DSC module (MODULES-5228)
2.1.1 - 2017-07-07
Added
- Rake tasks for release automation
- Experimental support for non-Windows Support (CentOS, Ubuntu) (MODULES-3945)
Fixed
- Updated documentation (DOC-2960)
- Updated metadata for Puppet 4 and Puppet 5 (MODULES-4528, MODULES-4822, MODULES-5144)
- Dispose runspace on pipe close (MODULES-4754)
- Removed rspec configuration for win32_console (MODULES-4976)
- Provider will now respect the environment parameter (MODULES-4138)
- Return available UI Output on error (MODULES-5145)
2.1.0 - 2016-11-17
Fixed
- Support for Windows 2016/WMF 5.1 using named pipes (MODULES-3690)
- Fixed documentation for herestring (DOC-2960)
Added
- Speed improvements to the PowerShell manager (MODULES-3690)
2.0.3 - 2016-10-05
Added
- The ability to set the current working directory (MODULES-3565)
Fixed
- Miscellaneous fixes to improve reliability
- Fixed capture exit codes when executing external scripts (MODULES-3399)
- Fixed respect user specified timeout (MODULES-3709)
- Improved handling of user code exceptions (MODULES-3443)
- Fixed output line and stacktrace of user code exception (MODULES-3839)
- Improved the PowerShell host so that it is more resilient to failure (MODULES-3875)
- Fixed race condition in threading with the PowerShell host (MODULES-3144)
- Modified tests to detect differences in PowerShell error text (MODULES-3443)
- Documented how to handle exit codes (MODULES-3588)
2.0.2 - 2016-07-12
Added
- Noticable speed increase by reducing the time start for a PowerShell command (MODULES-3406)
- Tests for try/catch (MODULES-2634)
Fixed
- Fixed minor bugs in tests (MODULES-3347)
- Fixed bug with older ruby (1.8)
2.0.1 - 2016-05-24
Fixed
- Updated the PowerShell manager so that it does not conflict with the PowerShell Manager in the Puppet DSC module (FM-5240)
2.0.0 - 2016-05-17
Changed
- Major performance improvement by sharing a single PowerShell session, instead of creating a new PowerShell session per command. This change no longer writes temporary scripts to file system. (MODULES-2962)
Fixed
- Updated test suites with later versions (MODULES-2452, MODULES-3011)
- Cleaned up documentation (MODULES-3192)
- Removed extra verbose output
1.0.6 - 2015-12-08
Fixed
- Fixed testing bug when testing on Puppet 3+ on Windows Server 2003 (MODULES-2443)
1.0.5 - 2015-07-28
Added
- Metadata for Puppet 4 and PE 2015.2.0 (FM-2752)
Fixed
- Minor testing bug fixes (MODULES-2207)
- Readme cleanup (DOC-1497)
1.0.4 2014-11-04
Fixed
- Fixed issues URL in metadata.json
Added
- Future Parser testing support (FM-1519)
1.0.3 - 2014-08-25
Fixed
- Updated tests to verify that PowerShell continues to function on x64-native ruby
1.0.2 - 2014-07-15
Fixed
- Updated metadata.json so that the module can be uninstalled and upgraded via the puppet module command
1.0.1
Fixed
- Fixed issue with metadata and PE version requirement
* This Changelog was automatically generated by github_changelog_generator
Dependencies
- puppetlabs/pwshlib (>= 0.4.0 < 2.0.0)
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Quality checks
We run a couple of automated scans to help you assess a module’s quality. Each module is given a score based on how well the author has formatted their code and documentation and select modules are also checked for malware using VirusTotal.
Please note, the information below is for guidance only and neither of these methods should be considered an endorsement by Puppet.
Malware scan results
The malware detection service on Puppet Forge is an automated process that identifies known malware in module releases before they’re published. It is not intended to replace your own virus scanning solution.
Learn more about malware scans- Module name:
- puppetlabs-powershell
- Module version:
- 6.0.0
- Scan initiated:
- April 24th 2023, 6:36:05
- Detections:
- 0 / 59
- Scan stats:
- 59 undetected
- 0 harmless
- 0 failures
- 0 timeouts
- 0 malicious
- 0 suspicious
- 15 unsupported
- Scan report:
- View the detailed scan report