diff options
Diffstat (limited to 'lib/chef/provider/powershell_script.rb')
-rw-r--r-- | lib/chef/provider/powershell_script.rb | 146 |
1 files changed, 73 insertions, 73 deletions
diff --git a/lib/chef/provider/powershell_script.rb b/lib/chef/provider/powershell_script.rb index 5af73b8b69..6117fa4e1c 100644 --- a/lib/chef/provider/powershell_script.rb +++ b/lib/chef/provider/powershell_script.rb @@ -85,10 +85,10 @@ class Chef # in that block will still trigger a syntax error which is # exactly what we want here -- verify the syntax without # actually running the script. - user_code_wrapped_in_powershell_script_block = <<-EOH -{ - #{new_resource.code} -} + user_code_wrapped_in_powershell_script_block = <<~EOH + { + #{new_resource.code} + } EOH user_script_file.puts user_code_wrapped_in_powershell_script_block @@ -146,75 +146,75 @@ EOH # executed, otherwise 0 or 1 based on whether $? is set to true # (success, where we return 0) or false (where we return 1). def wrapper_script - <<-EOH -# Chef Client wrapper for powershell_script resources - -# In rare cases, such as when PowerShell is executed -# as an alternate user, the new-variable cmdlet is not -# available, so import it just in case -if ( get-module -ListAvailable Microsoft.PowerShell.Utility ) -{ - Import-Module Microsoft.PowerShell.Utility -} - -# LASTEXITCODE can be uninitialized -- make it explictly 0 -# to avoid incorrect detection of failure (non-zero) codes -$global:LASTEXITCODE = 0 - -# Catch any exceptions -- without this, exceptions will result -# In a zero return code instead of the desired non-zero code -# that indicates a failure -trap [Exception] {write-error ($_.Exception.Message);exit 1} - -# Variable state that should not be accessible to the user code -new-variable -name interpolatedexitcode -visibility private -value $#{new_resource.convert_boolean_return} -new-variable -name chefscriptresult -visibility private - -# Initialize a variable we use to capture $? inside a block -$global:lastcmdlet = $null - -# Execute the user's code in a script block -- -$chefscriptresult = -{ - #{new_resource.code} - - # This assignment doesn't affect the block's return value - $global:lastcmdlet = $? -}.invokereturnasis() - -# Assume failure status of 1 -- success cases -# will have to override this -$exitstatus = 1 - -# If convert_boolean_return is enabled, the block's return value -# gets precedence in determining our exit status -if ($interpolatedexitcode -and $chefscriptresult -ne $null -and $chefscriptresult.gettype().name -eq 'boolean') -{ - $exitstatus = [int32](!$chefscriptresult) -} -elseif ($lastcmdlet) -{ - # Otherwise, a successful cmdlet execution defines the status - $exitstatus = 0 -} -elseif ( $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0 ) -{ - # If the cmdlet status is failed, allow the Win32 status - # in $LASTEXITCODE to define exit status. This handles the case - # where no cmdlets, only Win32 processes have run since $? - # will be set to $false whenever a Win32 process returns a non-zero - # status. - $exitstatus = $LASTEXITCODE -} - -# Print STDOUT for the script execution -Write-Output $chefscriptresult - -# If this script is launched with -File, the process exit -# status of PowerShell.exe will be $exitstatus. If it was -# launched with -Command, it will be 0 if $exitstatus was 0, -# 1 (i.e. failed) otherwise. -exit $exitstatus + <<~EOH + # Chef Client wrapper for powershell_script resources + + # In rare cases, such as when PowerShell is executed + # as an alternate user, the new-variable cmdlet is not + # available, so import it just in case + if ( get-module -ListAvailable Microsoft.PowerShell.Utility ) + { + Import-Module Microsoft.PowerShell.Utility + } + + # LASTEXITCODE can be uninitialized -- make it explictly 0 + # to avoid incorrect detection of failure (non-zero) codes + $global:LASTEXITCODE = 0 + + # Catch any exceptions -- without this, exceptions will result + # In a zero return code instead of the desired non-zero code + # that indicates a failure + trap [Exception] {write-error ($_.Exception.Message);exit 1} + + # Variable state that should not be accessible to the user code + new-variable -name interpolatedexitcode -visibility private -value $#{new_resource.convert_boolean_return} + new-variable -name chefscriptresult -visibility private + + # Initialize a variable we use to capture $? inside a block + $global:lastcmdlet = $null + + # Execute the user's code in a script block -- + $chefscriptresult = + { + #{new_resource.code} + + # This assignment doesn't affect the block's return value + $global:lastcmdlet = $? + }.invokereturnasis() + + # Assume failure status of 1 -- success cases + # will have to override this + $exitstatus = 1 + + # If convert_boolean_return is enabled, the block's return value + # gets precedence in determining our exit status + if ($interpolatedexitcode -and $chefscriptresult -ne $null -and $chefscriptresult.gettype().name -eq 'boolean') + { + $exitstatus = [int32](!$chefscriptresult) + } + elseif ($lastcmdlet) + { + # Otherwise, a successful cmdlet execution defines the status + $exitstatus = 0 + } + elseif ( $LASTEXITCODE -ne $null -and $LASTEXITCODE -ne 0 ) + { + # If the cmdlet status is failed, allow the Win32 status + # in $LASTEXITCODE to define exit status. This handles the case + # where no cmdlets, only Win32 processes have run since $? + # will be set to $false whenever a Win32 process returns a non-zero + # status. + $exitstatus = $LASTEXITCODE + } + + # Print STDOUT for the script execution + Write-Output $chefscriptresult + + # If this script is launched with -File, the process exit + # status of PowerShell.exe will be $exitstatus. If it was + # launched with -Command, it will be 0 if $exitstatus was 0, + # 1 (i.e. failed) otherwise. + exit $exitstatus EOH end |