PowerShell从命令行参数中删除双引号 [英] PowerShell stripping double quotes from command line arguments

查看:1089
本文介绍了PowerShell从命令行参数中删除双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在使用GnuWin32从PowerShell使用GnuWin32每当双引号涉及。

Recently I have been having some trouble using GnuWin32 from PowerShell whenever double quotes are involved.

进一步调查,似乎PowerShell剥离双引号从命令行参数,即使正确转义。

Upon further investigation, it appears PowerShell is stripping double quotes from command line arguments, even when properly escaped.

PS C:\Documents and Settings\Nick> echo '"hello"'
"hello"
PS C:\Documents and Settings\Nick> echo.exe '"hello"'
hello
PS C:\Documents and Settings\Nick> echo.exe '\"hello\"'
"hello"

双引号传递到PowerShell的 echo cmdlet,但是作为参数传递给 echo.exe 时,双引号被剥离,除非用反斜杠转义PowerShell的转义字符是反引号,而不是反斜杠。)

Notice that the double quotes are there when passed to PowerShell's echo cmdlet, but when passed as an argument to echo.exe, the double quotes are stripped unless escaped with a backslash (even though PowerShell's escape character is a backtick, not a backslash).

这看起来像是一个bug。如果我将正确的转义字符串传递给PowerShell,则PowerShell应该处理任何转义可能是必要的,因为它调用该命令。

This seems like a bug to me. If I am passing the correct escaped strings to PowerShell, then PowerShell should take care of whatever escaping may be necessary for however it invokes the command.

这里发生了什么?

现在,修复是根据这些规则转义命令行参数(似乎被 CreateProcess PowerShell用于调用.exe文件的API调用):

For now, the fix is to escape command line arguments in accordance with these rules (which seem to be used by the CreateProcess API call which PowerShell uses to invoke .exe files):


  • 要传递双引号,使用反斜杠转义: \ - >

  • 传递一个或多个反斜杠双引号,将每个反斜杠转换为另一个反斜杠并转义引号: \\\\\ - > \ \\

  • 如果后面没有双引号,则不必对反斜杠进行转义: \\ - > \\

  • To pass a double quote, escape with a backslash: \" -> "
  • To pass a one or more backslashes followed by a double quote, escape each backslash with another backslash and escape the quote: \\\\\" -> \\"
  • If not followed by a double quote, no escaping is necessary for backslashes: \\ -> \\

请注意,

以下是一些示例,其中 echo.exe 可以用来将Windows API转义字符串中的双引号转换为PowerShell。 GnuWin32:

Here are some examples, with echo.exe from GnuWin32:

PS C:\Documents and Settings\Nick> echo.exe "\`""
"
PS C:\Documents and Settings\Nick> echo.exe "\\\\\`""
\\"
PS C:\Documents and Settings\Nick> echo.exe "\\"
\\

如果你需要传递一个复杂的命令行参数,可以迅速变得地狱。当然,在 CreateProcess()或PowerShell文档中没有记录。

I imagine that this can quickly become hell if you need to pass a complicated command line parameter. Of course, none of this documented in the CreateProcess() or PowerShell documentation.

必须将带有双引号的参数传递给.NET函数或PowerShell cmdlet。为此,您只需要将双引号转义到PowerShell。

Also note that this is not necessary to pass arguments with double quotes to .NET functions or PowerShell cmdlets. For that, you need only escape your double quotes to PowerShell.

推荐答案

这是一个已知事物


FAR TOO HARD将参数传递给需要引用字符串的应用程序。我在IRC中用一个空间的PowerShell专家问了这个问题,花了一个小时,有人找出一种方法(我最初开始发布,这是根本不可能)。这完全打破了PowerShell作为通用shell的能力,因为我们不能做简单的事情,如执行sqlcmd。命令shell的第一个作业应该运行命令行应用程序...作为示例,尝试从SQL Server 2008使用SqlCmd,有一个-v参数,它需要一系列name:value参数。如果值中有空格,你必须引用它...

It's FAR TOO HARD to pass parameters to applications which require quoted strings. I asked this question in IRC with a "roomful" of PowerShell experts, and it took hour for someone to figure out a way (I originally started to post here that it is simply not possible). This completely breaks PowerShell's ability to serve as a general purpose shell, because we can't do simple things like executing sqlcmd. The number one job of a command shell should be running command-line applications... As an example, trying to use SqlCmd from SQL Server 2008, there is a -v parameter which takes a series of name:value parameters. If the value has spaces in it, you must quote it...

...没有单一的方法来编写命令行来正确地调用这个应用程序,所以即使你掌握了所有4或5种不同方式的引用和转义的东西,你仍然猜测什么将工作,当...或者,你可以只是shell out的cmd,并完成它。

...there is no single way to write a command line to invoke this application correctly, so even after you master all 4 or 5 different ways of quoting and escaping things, you're still guessing as to which will work when ... or, you can just shell out to cmd, and be done with it.

这篇关于PowerShell从命令行参数中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆