The PowerShell gem.
This gem enables you to execute PowerShell from within ruby without having to instantiate and tear down a PowerShell process for each command called. It supports Windows PowerShell as well as PowerShell Core (and, soon, just PowerShell) - 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.
Add this line to your application's Gemfile:
gem 'ruby-pwsh'
And then execute:
bundle install
Or install it yourself as:
gem install ruby-pwsh
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.
pp(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.
pp(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
pp("The PowerShell version of the currently running Manager is #{ps_version}")
You can find the full reference documentation online, here.
Steps to release an update to the gem and module include:
- Ensure that the release branch is up to date with the master:
git push upstream upstream/master:release --force
- Checkout a new working branch for the release prep (where xyz is the appropriate version, sans periods):
git checkout -b maint/release/prep-xyz upstream/release
- Update the version in
lib/pwsh/version.rb
andmetadata.json
to the appropriate version for the new release. - Run the changelog update task (make sure to verify the changelog, correctly tagging PRs as needed):
bundle exec rake changelog
- Commit your changes with a short, sensible commit message, like:
git add lib/pwsh/version.rb git add metadata.json git add CHANGELOG.md git commit -m '(MAINT) Prep for x.y.z release'
- Push your changes and submit a pull request for review against the release branch:
git push -u origin maint/release-prep-xyz
- Ensure tests pass and the code is merged to
release
. - Grab the commit hash from the merge commit on release, use that as the tag for the version (replacing
x.y.z
with the appropriate version andcommithash
with the relevant one), then push the tags to upstream:bundle exec rake tag['x.y.z', 'commithash']
- Build the Ruby gem and publish:
bundle exec rake build bundle exec rake push['ruby-pwsh-x.y.z.gem']
- Verify that the correct version now exists on RubyGems
- Build the Puppet module:
bundle exec rake build_module
- Publish the updated module version (found in the
pkg
folder) to the Forge. - Submit the mergeback PR from the release branch to master.