Skip to content

Commit

Permalink
(maint) Update powershell helpers to escape correctly for linux
Browse files Browse the repository at this point in the history
This commit updates the PuppetBolt powershell module to correctly escape hash
parameters for linux platforms. Conversation in this thread:
MicrosoftDocs/PowerShell-Docs#2361 describe the
circumstances of why interpolation breaks.

The fix is to include backslash escape chars for all quotes in hash parameters
if the powershell function is running on linux

!no-release-note
  • Loading branch information
mcdonaldseanp committed Feb 16, 2021
1 parent 85a66b2 commit 26cd001
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pwsh_module/pwsh_bolt_internal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ function Get-BoltCommandline {
Write-Verbose "Parsing $($kvp.key) as hashtable parameter"
$v = ConvertTo-Json -InputObject $pwshValue -Compress
$params += "--$($rubyParameter)"
$params += "'$($v)'"
# Commands run on Linux platforms will fail because the
# the interpolation of the command from powershell -> .NET -> Ruby
# will end up stripping out quotes that aren't baskslash-escaped.
#
# See https://github.com/MicrosoftDocs/PowerShell-Docs/issues/2361
#
# $IsLinux is an automatic variable from powershellcore:
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1#islinux
if ($IsLinux) {
$params += $v -replace '"', '\"'
} else {
$params += "'$($v)'"
}
}
elseif ($pwshValue.GetType().Name -eq 'List`1') {
Write-Verbose "Parsing $($kvp.key) as array parameter"
Expand Down

0 comments on commit 26cd001

Please sign in to comment.