lib/beaker-windows/powershell.rb
# Executes a PowerShell command on a host. Allow validation of command execution and fail if
# PowerShell command throws an exception. Note: if quotes are required then the single quote
# should be preferred. If double quotes are required you will need to double escape the quotes!
#
# ==== Attributes
#
# * +command+ - The PowerShell command to execute on the host.
# * +opts+ - Either command-line options to pass to PowerShell or the following:
#   * +:excep_fail+ - Command will return exit code 1 if an exception is caught.
#     (Default: true)
#   * +:verify_cmd+ - Command will return exit code 1 if the outcome of the command
#     is "$false". (Default: false)
#   * +:EncodedCommand+ - Will encode the command in base64. Useful for commands
#      with nested quoting or containing Unicode characters. (Default: false)
#
# ==== Returns
#
# +Beaker::Command+ - An object for executing powershell commands on a host
#
# ==== Raises
#
# +nil+
#
# ==== Example
#
# on(hosts, exec_ps_cmd("Set-Content -path 'fu.txt' -value 'fu'"))
# on(hosts, exec_ps_cmd("Set-Content -path 'fu.txt' -value 'fu'", :ExecutionPolicy => 'Unrestricted')
# on(hosts, exec_ps_cmd("Set Content -path 'fu.txt', -value 'fu'", :EncodedCommand => true))
# on(hosts, exec_ps_cmd("1 -eq 2", :verify_cmd => true))
# on(hosts, exec_ps_cmd("does.not.exist", :excep_fail => true))
Executes a PowerShell command on a host. Allow validation of command execution and fail if
PowerShell command throws an exception. Note: if quotes are required then the single quote
should be preferred. If double quotes are required you will need to double escape the quotes!

Attributes

+command+ - The PowerShell command to execute on the host.
+opts+ - Either command-line options to pass to PowerShell or the following:
+:excep_fail+ - Command will return exit code 1 if an exception is caught.
(Default: true)
+:verify_cmd+ - Command will return exit code 1 if the outcome of the command
is "$false". (Default: false)
+:EncodedCommand+ - Will encode the command in base64. Useful for commands
with nested quoting or containing Unicode characters. (Default: false)

Returns

+Beaker::Command+ - An object for executing powershell commands on a host

Raises



Example


Executes a PowerShell command on a host . Allow validation of command execution and fail if PowerShell command throws an exception . Note : if quotes are required then the single quote should be preferred . If double quotes are required you will need to double escape the quotes! Attributes + command + - The PowerShell command to execute on the host . + opts + - Either command - line options to pass to PowerShell or the following : + : excep_fail + - Command will return exit code 1 if an exception is caught . ( Default : true ) + : verify_cmd + - Command will return exit code 1 if the outcome of the command is " $false " . ( Default : false ) + : EncodedCommand + - Will encode the command in base64 . Useful for commands with nested quoting or containing Unicode characters . ( Default : false ) Returns + Beaker :: Command + - An object for executing powershell commands on a host Raises Example
def exec_ps_cmd(command, opts={})
      opts[:verify_cmd] ||= false
      opts[:excep_fail] = true if opts[:excep_fail].nil?
      ps_opts = {} 
      if opts[:verify_cmd]
        command = "if ( #{command} ) { exit 0 } else { exit 1 }"
      end
      if opts[:excep_fail]
        var_accessor = opts[:EncodedCommand] ? '$_' : '\$_'
        command = "try { #{command} } catch { Write-Host #{var_accessor}.Exception.Message; exit 1 }"
      end
      command = "\"#{command}\"" unless opts[:EncodedCommand]
      opts.each do |k, v|
        ps_opts[k.to_s] = v unless (k == :verify_cmd) || (k == :excep_fail)
      end
      return powershell(command, ps_opts)
    end
def exec_ps_cmd ( command , opts = { } ) opts [ :verify_cmd ] ||= false opts [ :excep_fail ] = true if opts [ :excep_fail ] . nil? ps_opts = { } if opts [ :verify_cmd ] command = "if ( #{command} ) { exit 0 } else { exit 1 }" end if opts [ :excep_fail ] var_accessor = opts [ :EncodedCommand ] ? '$_' : '\$_' command = "try { #{command} } catch { Write-Host #{var_accessor}.Exception.Message; exit 1 }" end command = "\"#{command}\"" unless opts [ :EncodedCommand ] opts . each do | k , v | ps_opts [ k . to_s ] = v unless ( k == :verify_cmd ) || ( k == :excep_fail ) end return powershell ( command , ps_opts ) end
Executes a PowerShell command on a host.
Executes a PowerShell command on a host .
# Init # Command-line options to pass to PowerShell # Determine how the command should be wrapped for execution # Only prefix escape character if not encoded # Wrap the command in quotes when not encoded # Coerce all keys to be strings.
command
<not_specific>
opts
<not_specific>