Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the ruby function 'powershell' defined?

I am using the msutter DSC module for puppet. While reading through the source code, I come across code like this (in dsc_configuration_provider.rb):

  def create
    Puppet.debug "\n" + ps_script_content('set')
    output = powershell(ps_script_content('set'))
    Puppet.debug output
  end

What file defines the powershell function or method? Is it a ruby builtin? A puppet builtin? Inherited from a class? I know that it is being used to send text to powershell as a command and gathering results, but I need to see the source code to understand how to improve its error logging for my purposes, because certain powershell errors are being swallowed and no warnings are being printed to the Puppet log.

These lines in file dsc_provider_helpers.rb may be relevant:

    provider.commands :powershell =>
    if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
      "#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
    elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
      "#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
    else
      'powershell.exe'
    end

Surely this defines where the Powershell executable is located, but gives no indication how it is called and how its return value is derived. Are stdout and stderr combined? Am I given the text output or just the error code? etc.

like image 481
Paul Chernoch Avatar asked Mar 30 '26 20:03

Paul Chernoch


1 Answers

This is core Puppet logic. When a provider has a command, like

commands :powershell => some binary

That is hooked up as a function powershell(*args).

You can see it with other providers like Chocolatey:

  commands :chocolatey => chocolatey_command

  def self.chocolatey_command
    if Puppet::Util::Platform.windows?
      # must determine how to get to params in ruby
      #default_location = $chocolatey::params::install_location || ENV['ALLUSERSPROFILE'] + '\chocolatey'
      chocopath = ENV['ChocolateyInstall'] ||
          ('C:\Chocolatey' if File.directory?('C:\Chocolatey')) ||
          ('C:\ProgramData\chocolatey' if File.directory?('C:\ProgramData\chocolatey')) ||
          "#{ENV['ALLUSERSPROFILE']}\chocolatey"

      chocopath += '\bin\choco.exe'
    else
      chocopath = 'choco.exe'
    end

    chocopath
  end

Then other locations can just call chocolatey like a function with args:

 chocolatey(*args)
like image 196
ferventcoder Avatar answered Apr 02 '26 11:04

ferventcoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!