Consider the following cmd.exe
batch script:
testpar.cmd
@echo [%1] [%2]
If we call it it in Powershell, these results are rather obvious:
PS> .\testpar.cmd arg1 arg2
[arg1] [arg2]
PS> .\testpar.cmd "arg1 arg2"
["arg1 arg2"] []
But, when passing a variable:
PS> $alist= "arg1 arg2"
PS> .\testpar.cmd $alist
["arg1 arg2"] []
It seems that $alist
is passed with quotes.
One solution:
PS> $alist= "`"arg1`" `"arg2`""
PS> .\testpar.cmd $alist
["arg1"] ["arg2"]
which identifies arguments, but without stripping quotes.
Another possibility:
PS> Invoke-Expression (".\testpar.cmd " + $alist)
[arg1] [arg2]
This works, but is very convoluted. Any better way?
In particular is there a way to unquote a string variable?