The actual cmd i want to use is more complex but has the same problem with speechmarks. Printing rp1 in the log gives:
"test : The term 'test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:14
+ $imageFile = test
+ CategoryInfo : ObjectNotFound: (test:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Which implies the line was ran without speechmarks.
The command line argument needs some additional quotation marks and escaping to make the powershell command line parsing work. (This is the second time I've used powershell, I may be overlooking something...) (Also, this web page syntax coloring is wrong for the square bracket quoting escape.)
command = "\["$imagefile = \"test\";echo $imagefile"]\";
Write( "\!nthe command is:", command ); // write shows it in the log without extra escapes
result = RunProgram(
executable( "powershell.exe" ), //
options( command ), //
readfunction( "text" )//
Show( result );
the command is:"$imagefile = \"test\";echo $imagefile" result = "test ";
Here's a screen capture of two tries I made in a CMD window.
powershell output in a CMD window
The outer quotation marks make powershell see a single option; the outer quotation marks are removed by CMD before powershell gets the text. In your original example, there were three items powershell received ($imagefile, =, test ) and the outer quotation marks around test were removed by CMD. The outer quotation marks are needed when there are quotation marks or spaces in the arguments.
Over-simplified: multiple arguments on a command line are separated by spaces. Each quotation mark toggles whether spaces are used to separate arguments. \\ is a literal \ and \" is a literal ". Two adjacent quotation marks when spaces are toggled off results in a literal ", but the spaces may be toggled on or off depending on which parser variation is used.