Packaged script doesn't handle parameter with Space and Backward slash characters

Issue #243 resolved
Madhu P. Nair created an issue

Input parameter with space and a backward slash char at the end results in unexpected errors or results with a packaged script. Below is simple script and some test examples to reproduce the issue. Issue occurs when there is one or more space in the parameter value and has a single backward slash at the end. Workaround is to remove the slash or add a slash at the end, which makes the exe handle the value correctly.

[CmdletBinding()] 
param 
(
    [Parameter(Mandatory = $true, ParameterSetName = "FirstParamSet")]
    [Parameter(Mandatory = $true, ParameterSetName = "ThirdParamSet")]
    [ValidateNotNullOrEmpty()]
    [String]$FirstParamSetFirstParam,

    [Parameter(Mandatory = $false, ParameterSetName = "FirstParamSet")]
    [ValidateNotNullOrEmpty()]
    [String]$FirstParamSetSecondParam,

    [Parameter(Mandatory = $true, ParameterSetName = "SecondParamSet")]
    [Parameter(Mandatory = $true, ParameterSetName = "FourthParamSet")]
    [ValidateNotNullOrEmpty()]
    [String]$SecondParamSetFirstParam,

    [Parameter(Mandatory = $false, ParameterSetName = "SecondParamSet")]
    [ValidateScript( { $_ -gt 0 })]
    [int]$SecondParamSetSecondParam
)

Write-Host "Value provided for First Parameter in the First Parameter Set:"  $FirstParamSetFirstParam
Write-Host "Value provided for Second Parameter in the First Parameter Set:"  $FirstParamSetSecondParam
Write-Host "Value provided for First Parameter in the Second Parameter Set:"  $SecondParamSetFirstParam
Write-Host "Value provided for Second Parameter in the Second Parameter Set:"  $SecondParamSetSecondParam

Some test runs that reproduces the issue

.\Test-ExeCommandLineBackslashIssue.exe -FirstParamSetFirstParam "Text With Space\" -FirstParamSetSecondParam "TextWithoutSpace"
Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ & { [CmdletBinding()]
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet

.\Test-ExeCommandLineBackslashIssue.exe -FirstParamSetFirstParam "C:\TextWithoutSpace\" -FirstParamSetSecondParam "C:\Text With Space"
Value provided for First Parameter in the First Parameter Set: C:\TextWithoutSpace\
Value provided for Second Parameter in the First Parameter Set: C:\Text With Space
Value provided for First Parameter in the Second Parameter Set:
Value provided for Second Parameter in the Second Parameter Set: 0

.\Test-ExeCommandLineBackslashIssue.exe -FirstParamSetFirstParam "C:\TextWithoutSpace\" -FirstParamSetSecondParam "C:\Text With Space\"
Value provided for First Parameter in the First Parameter Set: C:\TextWithoutSpace\
Value provided for Second Parameter in the First Parameter Set: C:\Text With Space"
Value provided for First Parameter in the Second Parameter Set:
Value provided for Second Parameter in the Second Parameter Set: 0

.\Test-ExeCommandLineBackslashIssue.exe -FirstParamSetFirstParam "C:\TextWithoutSpace\" -FirstParamSetSecondParam "C:\Text With Space\\"
Value provided for First Parameter in the First Parameter Set: C:\TextWithoutSpace\
Value provided for Second Parameter in the First Parameter Set: C:\Text With Space\
Value provided for First Parameter in the Second Parameter Set:
Value provided for Second Parameter in the Second Parameter Set: 0

Comments (5)

  1. Log in to comment