Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions InvokeHelperTest/public/Invoke-MyCommand.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,37 @@
Assert-AreEqual -Expected 'fakeName' -Presented $result.login
Assert-AreEqual -Expected 6666666 -Presented $result.id

}

function InvokeHelperTest_Invoke_MyCommand_WithDoubleQuotes {
[CmdletBinding()]
param()

$str = 'Hello "world"'
$command = 'echo "{string}"'

## Wrong call with no parameter formatting

# Arrange
$param = @{
string= $str
}
# Act
$result = Invoke-MyCommand -Command $command -Parameters $param

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Assert
Assert-AreNotEqual -Expected $str -Presented $result

# Correct call with parameter formatting

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Arrange
$param = @{
string= $str | ConvertTo-InvokeParameterString
}

# Act
$result = Invoke-MyCommand -Command $command -Parameters $param

# Assert
Assert-AreEqual -Expected $str -Presented $result
}
20 changes: 20 additions & 0 deletions public/Format-InvokeParameterString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

# Invoke command use powershell calls to manage the dependencies
# if a string contains double quotes, it will fail as it will confuse the string content with parameter values
# so we need to escape the double quotes in the string to ensure the quotes that are part of the string value and not parameter boundaries

function ConvertTo-InvokeParameterString {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'ConvertTo-InvokeParameterString' does not have a help comment. Note

The cmdlet 'ConvertTo-InvokeParameterString' does not have a help comment.
param (
[Parameter(ValueFromPipeline, Position = 0)][string]$InputString
)

process{

if ([string]::IsNullOrEmpty($InputString)) {
return $InputString
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$OutputString = $InputString -replace '"', '""'
return $OutputString
}
} Export-ModuleMember -Function ConvertTo-InvokeParameterString
Loading