Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Need info on how to escape double quotes

Reply
 
Old 10-08-2008   #1 (permalink)
Dimitar Hristov


 
 

Need info on how to escape double quotes

Hi,

We are developing PowerShell cmdlets and stumbled upon a problem with double
quotes escaping. We are using CreateProcess method to start powershell
process and run a nested powershell process in it. The arguments that we pass
to powershell.exe are the following:
-------------------------------
-NonInteractive -Command & {powershell.exe -NonInteractive -Command '&
{"aaa" -eq "bbb"}' > "C:\output.txt"}
-------------------------------
The idea is that we need the inner powershell to execute some user-defined
script (in this case: “aaa” –eq “bbb”). The outer powershell is needed in
order to redirect the output from the inner one in a file. But when we
execute this we receive the following error in the output.txt file:
-------------------------------
The term 'aaa' is not recognized as a cmdlet, function, operable program, or
script file. Verify the term and try again.
At line:1 char:7
+ & {aaa <<<< -eq bbb}
-------------------------------
We know that we need to escape the double quotes in some way, but we are
unsure exactly how. It appears that the standard escaping rules do not apply
in this case. Any help would be appreciated.

P.S. Instead of using CreateProcess the same result can be reproduced when
invoking the following line from powershell:
-------------------------------
powershell.exe -NonInteractive -Command {powershell.exe -NonInteractive
-Command '& {"aaa" -eq "bbb"}' > "C:\output.txt"}
-------------------------------

My System SpecsSystem Spec
Old 10-08-2008   #2 (permalink)
Kiron


 
 

Re: Need info on how to escape double quotes

Sorry I can't give you any developer help, I'm not one, but this works on CTP2 v2; I used the continuation character ` to avoid word wrapping, but it could be one line.
Also used short names for parameters for the same reason.

# CTP2 v2
powershell.exe -nonI -c {powershell.exe -nonI -c {& {"aaa" -eq "bbb"}`
Quote:

> C:\output.txt}}
# you can also encode the command and pass it to PowerShell.exe's
# -EncodedCommand parameter
$cmd = 'powershell.exe -nonI -c {& {"aaa" -eq "bbb"} > C:\output.txt}'
$encCmd = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($cmd))
powershell.exe -NonInteractive -encodedCommand $encCmd

--
Kiron
My System SpecsSystem Spec
Old 10-09-2008   #3 (permalink)


Windows XP 32bit
 
 

Re: Need info on how to escape double quotes

Thanks very much for your help. I'm using PowerShell 1.0 and encoding the command worked out perfectly.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Remove double quotes from a string .NET General
shortcuts targetpath has double quotes and won't work? PowerShell
Need info on how to escape double quotes PowerShell
here-string with single and double quotes PowerShell
Mix single and double quotes PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46