Version information
This version is compatible with:
- Puppet Enterprise 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, 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x, 2019.0.x
- Puppet >= 6.0.0 < 8.0.0
- , , , , , ,
Start using this module
Add this module to your Puppetfile:
mod 'puppetlabs-pwshlib', '0.10.1'
Learn more about managing modules with a PuppetfileDocumentation
pwshlib
This module enables you to leverage the ruby-pwsh
gem to execute PowerShell from within your Puppet providers without having to instantiate and tear down a PowerShell process for each command called.
It supports Windows PowerShell as well as PowerShell Core - if you're running PowerShell v3+, this gem supports you.
The Manager
class enables you to execute and interoperate with PowerShell from within ruby, leveraging the strengths of both languages as needed.
Prerequisites
Include puppetlabs-pwshlib
as a dependency in your module and you can leverage it in your providers by using a requires statement, such as in this example:
require 'puppet/resource_api/simple_provider'
begin
require 'ruby-pwsh'
rescue LoadError
raise 'Could not load the "ruby-pwsh" library; is the dependency module puppetlabs-pwshlib installed in this environment?'
end
# Implementation for the foo type using the Resource API.
class Puppet::Provider::Foo::Foo < Puppet::ResourceApi::SimpleProvider
def get(context)
context.debug("PowerShell Path: #{Pwsh::Manager.powershell_path}")
context.debug('Returning pre-canned example data')
[
{
name: 'foo',
ensure: 'present',
},
{
name: 'bar',
ensure: 'present',
},
]
end
def create(context, name, should)
context.notice("Creating '#{name}' with #{should.inspect}")
end
def update(context, name, should)
context.notice("Updating '#{name}' with #{should.inspect}")
end
def delete(context, name)
context.notice("Deleting '#{name}'")
end
end
Aside from adding it as a dependency to your module metadata, you will probably also want to include it in your .fixtures.yml
file:
fixtures:
forge_modules:
pwshlib: "puppetlabs/pwshlib"
Using the Library
Instantiating the manager can be done using some defaults:
# Instantiate the manager for Windows PowerShell, using the default path and arguments
# Note that this takes a few seconds to instantiate.
posh = Pwsh::Manager.instance(Pwsh::Manager.powershell_path, Pwsh::Manager.powershell_args)
# If you try to create another manager with the same arguments it will reuse the existing one.
ps = Pwsh::Manager.instance(Pwsh::Manager.powershell_path, Pwsh::Manager.powershell_args)
# Note that this time the return is very fast.
# We can also use the defaults for PowerShell Core, though these only work if PowerShell is
# installed to the default paths - if it is installed anywhere else, you'll need to specify
# the full path to the pwsh executable.
pwsh = Pwsh::Manager.instance(Pwsh::Manager.pwsh_path, Pwsh::Manager.pwsh_args)
Execution can be done with relatively little additional work - pass the command string you want executed:
# Instantiate the Manager:
posh = Pwsh::Manager.instance(Pwsh::Manager.powershell_path, Pwsh::Manager.powershell_args)
# Pretty print the output of `$PSVersionTable` to validate the version of PowerShell running
# Note that the output is a hash with a few different keys, including stdout.
Puppet.debug(posh.execute('$PSVersionTable'))
# Lets reduce the noise a little and retrieve just the version number:
# Note: We cast to a string because PSVersion is actually a Version object.
Puppet.debug(posh.execute('[String]$PSVersionTable.PSVersion'))
# We could store this output to a ruby variable if we wanted, for further use:
ps_version = posh.execute('[String]$PSVersionTable.PSVersion')[:stdout].strip
Puppet.debug("The PowerShell version of the currently running Manager is #{ps_version}")
For more information, please review the online reference documentation for the gem.
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.
0.10.1 (2021-08-23)
Fixed
- (GH-180) Ensure instance_key respects full uniqueness of options #181 (michaeltlombardi)
- (GH-165) Ensure null-value nested cim instance arrays are appropriately munged #177 (michaeltlombardi)
0.10.0 (2021-07-02)
Added
- (GH-172) Enable use of class-based DSC Resources by munging PSModulePath #173 (michaeltlombardi)
0.9.0 (2021-06-28)
Added
- (GH-147) Refactor Invocation methods to use shared helper and write error logs when appropriate #152 (david22swan)
- (GH-145) Improve DSC secrets redaction #150 (michaeltlombardi)
- (GH-145) Add insync? and invoke_test_method to dsc provider #124 (michaeltlombardi)
- (MAINT) Clarify supported platforms #113 (michaeltlombardi)
Fixed
- (IAC-1657) Fix for invalid DateTime value error in
invoke_get_method
#169 (david22swan) - (GH-154) Ensure values returned from
invoke_get_method
are recursively sorted in the DSC Base Provider to reduce canonicalization warnings. #160 (michaeltlombardi) - (GH-154) Fix return data from
Invoke-DscResource
for empty strings and single item arrays in DSC Base Provider #159 (michaeltlombardi) - (GH-155) Fix CIM Instance munging in
invoke_get_method
for DSC Base Provider #158 (michaeltlombardi) - (GH-154) Fix canonicalization in
get
method for DSC Base Provider #157 (michaeltlombardi) - (GH-144) Enable order-insensitive comparisons for DSC #151 (michaeltlombardi)
- (GH-143) Handle order insensitive arrays in the
same?
method of the DSC Base Provider #148 (michaeltlombardi) - (GH-127) Canonicalize enums correctly #131 (michaeltlombardi)
- (GH-125) Fix dsc provider canonicalization for absent resources #129 (michaeltlombardi)
- (MODULES-11051) Ensure environment variables are not incorrectly munged in the PowerShell Host #128 (michaeltlombardi)
- (MODULES-11026) Ensure the PowerShell manager works with v7 #122 (n3snah)
- (Maint) Ensure canonicalize correctly compares sorted hashes #118 (Hvid)
0.8.0 (2021-03-01)
0.7.4 (2021-02-12)
Fixed
- (GH-105) Ensure set runs on ambiguous ensure states #108 (michaeltlombardi)
- (GH-105) Ensure canonicalized_cache check validates against namevar #107 (michaeltlombardi)
0.7.3 (2021-02-03)
Fixed
0.7.2 (2021-02-03)
Fixed
- (GH-97) Memoize class variables in initialize #98 (michaeltlombardi)
- (MAINT) Ensure is_same check works for nil manifest values #96 (bwilcox)
0.7.1 (2021-02-02)
Fixed
- (MAINT) Correctly canonicalize enumerable values in dsc #92 (michaeltlombardi)
- (MAINT) Ensure vendored path check works with mix of module builds #91 (michaeltlombardi)
- (GH-84) Fix empty array parameter check #90 (michaeltlombardi)
- (MAINT) Minor fixes to CIM instance handling #89 (michaeltlombardi)
0.7.0 (2021-01-20)
Added
Fixed
- Make root module path use puppetized module name #86 (michaeltlombardi)
0.6.3 (2020-12-16)
Fixed
- (MAINT) Add handling for when dsc_ensure is stripped #78 (michaeltlombardi)
0.6.2 (2020-12-09)
Fixed
- (MAINT) Ensure parameters are canonicalized #75 (michaeltlombardi)
0.6.1 (2020-11-25)
Fixed
- (maint) - Removal of inappropriate terminology #70 (pmcmaw)
- (Maint) Fix ensurability in the dsc base provider #69 (michaeltlombardi)
0.6.0 (2020-11-24)
Added
- (GH-81) Handle parameters in the dsc base provider #62 (michaeltlombardi)
- (GH-74) Remove special handling for ensure in the dsc base provider #61 (michaeltlombardi)
- (GH-59) Refactor away from Simple Provider #60 (michaeltlombardi)
Fixed
- (GH-57) Handle datetimes in dsc #58 (michaeltlombardi)
- (GH-55) Handle intentionally empty arrays #56 (michaeltlombardi)
0.5.1 (2020-09-25)
Fixed
- (MAINT) Ensure dsc provider finds dsc resources during agent run #45 (michaeltlombardi)
0.5.0 (2020-08-20)
Added
- (IAC-1045) Add the DSC base Puppet provider to pwshlib #39 (michaeltlombardi)
0.4.1 (2020-02-13)
Fixed
0.4.0 (2020-01-14)
Added
- (MODULES-10389) Add puppet feature for dependent modules to leverage #20 (sanfrancrisko)
0.3.0 (2019-12-04)
Added
- (FEAT) Add method for symbolizing hash keys #16 (michaeltlombardi)
Fixed
- (FEAT) Ensure hash key casing methods work on arrays #15 (michaeltlombardi)
0.2.0 (2019-11-26)
Added
- (FEAT) Add quality of life utilities #11 (michaeltlombardi)
- (FM-8422) Make library releasable as a Puppet module #8 (michaeltlombardi)
* This Changelog was automatically generated by github_changelog_generator