Module: Chef::Mixin::PowershellExec

Instance Method Summary collapse

Instance Method Details

#powershell_exec(script, interpreter = :powershell, timeout: -1)) ⇒ Chef::PowerShell

Run a command under PowerShell via a managed (.NET) API.

Requires: .NET Framework 4.0 or higher on the target machine.

Parameters:

  • script (String)

    script to run

  • interpreter (Symbol) (defaults to: :powershell)

    the interpreter type, ‘:powershell` or `:pwsh`

  • timeout (Integer, nil) (defaults to: -1))

    timeout in seconds.

Returns:



109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/mixin/powershell_exec.rb', line 109

def powershell_exec(script, interpreter = :powershell, timeout: -1)
  case interpreter
  when :powershell
    Chef::PowerShell.new(script, timeout: timeout)
  when :pwsh
    Chef::Pwsh.new(script, timeout: timeout)
  else
    raise ArgumentError, "Expected interpreter of :powershell or :pwsh"
  end
end

#powershell_exec!(script, interpreter = :powershell, **options) ⇒ Object

The same as the #powershell_exec method except this will raise Chef::PowerShell::CommandFailed if the command fails



122
123
124
125
126
# File 'lib/chef/mixin/powershell_exec.rb', line 122

def powershell_exec!(script, interpreter = :powershell, **options)
  cmd = powershell_exec(script, interpreter, **options)
  cmd.error!
  cmd
end