class Mixlibrary::Core::Shell::Scripts::Powershell

Constants

EXIT_STATUS_EXCEPTION_HANDLER
EXIT_STATUS_EXITCONVERSION
EXIT_STATUS_INTIALIZATION

Make this set-variable call a read only. Technically can be overridden but most of PS scripts that set this setting, usually try to set this by doing things like $ERRORACTIONPREFERENCE=“Stop” Generally speaking this is the best way to handle this situation. We will need to determine if there are many dependent scripts that depend on being able to set this setting.

EXIT_STATUS_POST
EXIT_STATUS_RESET_SCRIPT

Public Class Methods

new(script, validate, options, flags, architecture = nil) click to toggle source
Calls superclass method
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 16
def initialize(script, validate, options, flags, architecture = nil)
  super(architecture)
  @originalScript=script
  @options = options
  @validate=validate
  @flagoverrides=flags
end

Public Instance Methods

orchestrate() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 24
def orchestrate
  #Powershell kind of sucks with syntax checking.
  #If any part of the script syntactically fails it will exit with a clean exit code of 0
  #So this forced our hand to call powershell twice for every script call.
  #1 - Put passed in script in a function to see if any standard error is generated -
  #only will happen if syntax is incorrect since function is never called
  #2 - Run the user script with exception wraps to guarantee some exit status
  syntax_check()
  
  myscriptstring = finalscript()
  
  return run_command(shell,flags,filename,file_extension, myscriptstring, @options, @validate)
end

Private Instance Methods

file_extension() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 134
def file_extension
  return ".ps1"
end
filename() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 130
def filename
  return "tempPSchef"
end
finalscript() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 93
def finalscript
  changed_script =
    EXIT_STATUS_EXITCONVERSION +
    EXIT_STATUS_EXCEPTION_HANDLER +
    EXIT_STATUS_RESET_SCRIPT +
    EXIT_STATUS_INTIALIZATION +
    @originalScript + 
    EXIT_STATUS_POST
              
  return changed_script
end
flags() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 105
def flags
  flags = [
    # Hides the copyright banner at startup.
    "-NoLogo",
    # Does not present an interactive prompt to the user.
    "-NonInteractive",
    # Does not load the Windows PowerShell profile.
    "-NoProfile",
    # always set the ExecutionPolicy flag
    # see http://technet.microsoft.com/en-us/library/ee176961.aspx
    "-ExecutionPolicy RemoteSigned",
    # Powershell will hang if STDIN is redirected
    # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
    "-InputFormat None",

    "-File"
  ]
  return @flagoverrides if @flagoverrides != nil
  return flags.join(' ')
end
shell() click to toggle source
# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 126
def shell
  return "powershell.exe"
end
syntax_check() click to toggle source

Validate valid powershell was passed in.

# File lib/mixlibrary/core/shell/scripts/powershell.rb, line 81
def syntax_check 
  local_script_contents=("function testme(){ \n #{@originalScript.to_s} \n} \n")
  
  result = run_command(shell,flags,filename,file_extension, local_script_contents, @options, false)
  #puts result.inspect
  if(result.stderr.length >= 1 || result.exitstatus !=0)
    #puts "Standard Output: \n #{result.stdout}"
    #puts "Standard Error: \n #{result.stderr}"
    raise RuntimeError,"Did not syntactically pass the powershell check.  \n Standard out: #{result.stdout} \n Standard Error:#{result.stderr}"
  end
end