I’m having trouble running powershell with a script that should use a variable number of parameters.
The script file looks like this:
param( [string]$paramString )
$params = ConvertFrom-StringData $paramString
$params
Running the script directly in powershell yields the expected result:
[PS] C:\some\path>.\test.ps1 "a=foo `n b=bar `n c=moo"
Name Value
---- -----
c moo
a foo
b bar
Calling powershell from the command line with the same script and parameters shows this:
C:\some\path>powershell -nologo -file ./test.ps1 "a=foo `n b=bar `n c=moo"
Name Value
---- -----
a foo `n b=bar `n c=moo
It seems like the passed string is in some format so the ConvertFrom-StringData function is unable to parse it anymore.
Approach with
-Fileis problematic because the parameter is not evaluated by cmd (at it is in PowerShell) and new lines equivalents are not expanded, they are passed in literally.The problem can be solved if we still let PowerShell to do that by using
-Commandinstead:It is ugly: we have to double every inner
"in the command. But it works. (The command can be defined by several parameters; I just prefer to use a single parameter: this way looks simpler to me).