Escaping quotes and double quotes

后端 未结 3 1467
说谎
说谎 2020-11-29 04:11

How do I properly escape the quotes in the -param value in the following command line?

$cmd=\"\\\\server\\toto.exe -batch=B -param=\"sort1;parmt         


        
3条回答
  •  春和景丽
    2020-11-29 04:51

    Escaping parameters like that is usually source of frustration and feels a lot like a time wasted. I see you're on v2 so I would suggest using a technique that Joel "Jaykul" Bennet blogged about a while ago.

    Long story short: you just wrap your string with @' ... '@ :

    Start-Process \\server\toto.exe @'
    -batch=B -param="sort1;parmtxt='Security ID=1234'"
    '@
    

    (Mind that I assumed which quotes are needed, and which things you were attempting to escape.) If you want to work with the output, you may want to add the -NoNewWindow switch.

    BTW: this was so important issue that since v3 you can use --% to stop the PowerShell parser from doing anything with your parameters:

    \\server\toto.exe --% -batch=b -param="sort1;paramtxt='Security ID=1234'"
    

    ... should work fine there (with the same assumption).

提交回复
热议问题