Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
david707
Level III

Unable to parse speechmarks "" into powershell using RunProgram

Hi, I'm having problems with Executing powershell, for example:

cmd = EvalInsert(
"\[$imageFile = "test"]\");
						rp1 = RunProgram(Executable("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" ),
							Options(Eval(cmd)),
							ReadFunction("text")
						);

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.

How would I ensure speechmarks are ran correctly?

Thanks in advance for any help on this.

2 REPLIES 2
Highlighted
Craige_Hales
Staff (Retired)

Re: Unable to parse speechmarks "" into powershell using RunProgram

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 windowpowershell 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.

Craige
Highlighted
Craige_Hales
Staff (Retired)

Re: Unable to parse speechmarks "" into powershell using RunProgram

Just stumbled into a great explanation of the windows command line, quotation marks, escaping, and why it seems so hard.

http://www.windowsinspired.com/the-correct-way-to-quote-command-line-arguments/

http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-win...

http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-line-into-individual-argume...

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.

Craige
Article Labels

    There are no labels assigned to this post.