# class Chef::Provider::PowershellScript

### Constants

EXIT_STATUS_EXCEPTION_HANDLER
EXIT_STATUS_NORMALIZATION_SCRIPT
EXIT_STATUS_RESET_SCRIPT

### Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider::WindowsScript.new

    # File lib/chef/provider/powershell_script.rb, line 58
    def initialize (new_resource, run_context)
      super(new_resource, run_context, '.ps1')
      normalize_script_exit_status(new_resource.code)
    end

### Public Instance Methods

flags() click to toggle source

    # File lib/chef/provider/powershell_script.rb, line 63
    def flags
      default_flags = [
        "-NoLogo",
        "-NonInteractive",
        "-NoProfile",
        "-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",
        # Must use -File rather than -Command to launch the script
        # file created by the base class that contains the script
        # code -- otherwise, powershell.exe does not propagate the
        # error status of a failed Windows process that ran at the
        # end of the script, it gets changed to '1'.
        "-File"
      ]

      interpreter_flags = default_flags.join(' ')

      if ! (@new_resource.flags.nil?)
        interpreter_flags = [@new_resource.flags, interpreter_flags].join(' ')
      end

      interpreter_flags
    end

### Protected Instance Methods

normalize_script_exit_status( code ) click to toggle source

Process exit codes are strange with PowerShell. Unless you explicitly call exit in Powershell, the powershell.exe interpreter returns only 0 for success or 1 for failure. Since we’d like to get specific exit codes from executable tools run with Powershell, we do some work using the automatic variables $? and $LASTEXITCODE to return the process exit code of the last process run in the script if it is the last command executed, otherwise 0 or 1 based on whether $? is set to true (success, where we return 0) or false (where we return 1).

    # File lib/chef/provider/powershell_script.rb, line 39
          def normalize_script_exit_status( code )
            target_code = ( EXIT_STATUS_EXCEPTION_HANDLER +
                            EXIT_STATUS_RESET_SCRIPT +
                            "\n" +
                            code.to_s +
                            EXIT_STATUS_NORMALIZATION_SCRIPT )
            convert_boolean_return = @new_resource.convert_boolean_return
            @code = <<EOH
    new-variable -name interpolatedexitcode -visibility private -value $#{convert_boolean_return}
    new-variable -name chefscriptresult -visibility private
    $chefscriptresult = {
    #{target_code}
    }.invokereturnasis()
    if ($interpolatedexitcode -and $chefscriptresult.gettype().name -eq 'boolean') { exit [int32](!$chefscriptresult) } else { exit 0 }
    EOH
          end