I intend to use a powershell variable in a script to pass a string value to a command whereupon the command is executed with the parameters as defined in the string. I have been looking at this for a couple of hours now and I don't see what I am doing wrong. I have simplified as much as I can. In the hope of fresh eyes I attach an image, showing:
a. the script that I am using. It's three lines long.
$Parms = "-it amazonlinux2 /bin/sh"
Write-Output "& docker run $Parms"
& docker run $Parms
b. the PS command line where I execute the script.
PS C:\dev\gamelift\serversdk\build> ./test
c. the result of the script's execution
& docker run -it amazonlinux2 /bin/sh
unknown shorthand flag: ' ' in - amazonlinux2 /bin/sh
See 'docker run --help'.
d. the same command executed immediately at the prompt.
PS C:\dev\gamelift\serversdk\build> docker run -it amazonlinux2 /bin/sh
e. the result of the prompt command, which is correct
sh-4.2#
What magic is happening so that the Write-Output is not being interpreted as the command? I have tried:
a. docker run $Parms (without the &)
b. & docker run "$Parms" (in quotes)
c. $Parms = "amazonlinux2 /bin/sh" and & docker run -it $Parms (different error)
d. $Parms = "amazonlinux2" and & docker run -it $Parms /bin/sh (works)
e. escaping the hyphen (different error)
f. $Parms = "-it amazonlinux2 /bin/sh" and each of & docker run $($Parms), & docker run ${$Parms} and & docker run ${Parms}
I'm feeling a bit stupid. I can't see how the Write-Output is right but the call command is bork. It can't be that hard...