如何在PowerShell中转义特殊字符? [英] How to escape special characters in PowerShell?

查看:1049
本文介绍了如何在PowerShell中转义特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的PowerShell脚本运行时,它会提示用户输入密码参数.该密码可以包含任意数量的特殊字符,例如* \〜;(%?.:@/ 该密码然后用作.exe命令的参数,但由于某些特殊字符无法正确转义,因此通常不正确.

When my PowerShell script runs, it prompts the user for a password parameter. That password can contain any number of special characters like *\~;(%?.:@/ That password is then used as a parameter for a .exe command, but it is often incorrect due to some special characters not being escaped properly.

一个过去的密码示例是$(?-.?-(.我唯一需要转义的字符是'(',为了使它可以工作,我将其替换为'`('.但是,该密码现已过期.新密码类似于* \〜;〜(%?.:: @/ *注意:这些密码中也混有随机数字和字母,但已被修改.

An example past password was $(?-.?-(. The only characters I needed to escape was '(', which I replaced with '`(' to make it work. However, that password is now expired. The new password is something like *\~;~(%?.:@/ *NOTE: these passwords have random numbers and letters mixed into them as well, but have been redacted.

新密码中唯一没有出现的字符是* \〜;%:@/ 有没有一种简单的方法可以转义所有字符并按原样接受任何用户输入?如果没有,有人会帮我摆脱这些特殊字符吗?

The only characters in the new password NOT in the first are *\~;%:@/ Is there an easy way to escape all characters and just take any user input as it is? If not, would someone mind helping me escape these special characters?

param (
    [Parameter(Mandatory=$true)][string]$password
)

上面的代码作为脚本的开头,导致控制台提示用户输入.

The above code prefaces the script, causing the console to prompt for user input.

Invoke-Expression -Command "<path_to_exe> -install $user $password"

^这是使用该密码参数的命令

^this is the command that uses that password parameter

我在Stack Overflow,Reddit和其他各种编码论坛/博客上尝试了许多其他建议,但都没有奏效.任何帮助深表感谢!

I have tried many other suggestions on Stack Overflow, Reddit, and other various coding forums/blogs and none have worked. Any help is much appreciated!

推荐答案

您正在使用Invoke-Expression调用外部程序:

You're using Invoke-Expression to call an external program:

  • 没有没有理由这样做,并且

  • There's no reason to do that, and Invoke-Expression should generally be avoided: it causes quoting headaches (as in your case), but, more importantly, it can be a security risk and there are typically better solutions.

  • As an aside: Unfortunately, even with direct invocation there can be quoting challenges around empty-string arguments and arguments with embedded " chars. - see footnote [1] and this answer.

如果您改为直接调用外部程序 -包括PowerShell在内的任何外壳程序都可以这样做-您的问题很可能会消失: [1]

& <path_to_exe> -install $user $password

注意:&,PowerShell的)和/或通过变量引用(例如,$HOME\foo.exe)指定时才需要);否则,您可以按原样调用可执行文件(例如,要调用cmd.exe,请使用
cmd /c 'echo hi'之类的东西).

Note: &, PowerShell's call operator, is only needed if your executable's path is quoted (e.g, "C:\Program Files\foo.exe") and/or is specified via a variable reference (e.g., $HOME\foo.exe); otherwise, you can invoke the executable as-is (e.g., to invoke cmd.exe, use something like
cmd /c 'echo hi').

另外,如果您发现自己需要转义字符集 中的任何字符,请使用-replace

Separately, if you do ever find yourself needing to escape any of the characters in a set of characters, use -replace with a character class, [...]:

注意:对于向内部程序(如上所示)或PowerShell命令传递参数的传递,不是必需的;但是,由于PowerShell对传递给外部程序的参数值中嵌入的"字符的处理不当,您可能必须转义"字符(仅),如\" [1 ] .

Note: This is not necessary for passing arguments, neither to external programs, as shown above, nor to PowerShell commands; however, due to PowerShell's broken handling of " characters embedded in argument values passed to external programs, you may have to escape " characters (only), as \"[1].

PS> 'a*b\c~d;e(f%g?h.i:j@k/l' -replace '[*\\~;(%?.:@/]', '`$&'
a`*b`\c`~d`;e`(f`%g`?h`.i`:j`@k`/l  # all chars. inside [...] were `-escaped

注意:由于\即使在字符类内部也具有特殊含义,因此必须将其转义为\\-其他所有字符.照原样使用.

Note: Since \ has special meaning even inside a character class, it had to be escaped as \\ - all other chars. are used as-is.

有关-replace运算符的更多信息,请参见此答案.

For more information about the -replace operator, see this answer.

[1]仍然有一个 字符会引起问题:嵌入式".由于历史原因,PowerShell不会正确地将嵌入式"正确地传递给外部程序,并且烦人地需要手动\-转义-请参见此GitHub问题以获取详细信息. 应用于您的解决方案:& <path_to_exe> -install $user ($password -replace '"', '\"')

[1] There is one character that still causes problems: embedded ". For historical reasons, PowerShell does not properly pass embedded " correctly to external programs, and annoyingly requires manual \-escaping - see this GitHub issue for details. Applied to your solution:& <path_to_exe> -install $user ($password -replace '"', '\"')

这篇关于如何在PowerShell中转义特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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