如何在命令行Powershell参数中转义“&"号,分号和花括号? [英] How to escape ampersands, semicolons, and curly braces in command-line powershell params?

查看:825
本文介绍了如何在命令行Powershell参数中转义“&"号,分号和花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Powershell脚本生成带有数据库密码等的配置文件.此脚本的正常使用是生成和使用密码,但是转瞬之间,我需要访问开发人员的同一个数据库,因此需要将密码传递到Powershell脚本中.

I'm using a powershell script to generate a config file with database passwords and the like. The normal use of this script is to generate-and-use a password, but for a fleeting moment, I need to access a fellow developer's database, so I need to pass the password into the powershell script.

问题在于密码是乱码,其中包括&"号,分号和大括号.如何将其传递给Powershell的命令行调用? (此答案和此

The problem is that the password is gibberish that includes ampersands, semicolons, and curly braces. How can I pass this into a command-line invocation of powershell? (This answer and this other answer are helpful, but doesn't help with the semicolons and curly braces.)

这是我尝试过的:

powershell .\makeConfig.ps1 -password "xxx<xxxx;xxxxx&x.xxxx}xx/xxx" -otherparam 3

结果:错误消息:&字符是不允许的. &运算符保留供将来使用;将&"号换成双 引号(&")作为字符串的一部分传递.

Result: An error message: The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.

powershell .\makeConfig.ps1 -password "xxx<xxxx;xxxxx`"&`"x.xxxx}xx/xxx" -otherparam 3

结果:对$ password进行写主机只会使字符串变成分号.另外,-otherparam具有默认值,而不是我传递给命令行的值.

Result: Doing a write-host of the $password only gives the string up to the semicolon. Also, -otherparam has its default value rather than the value that I passed in to the command line.

powershell .\makeConfig.ps1 -password "xxx<xxxx`;xxxxx`"&`"x.xxxx}xx/xxx" -otherparam 3

结果:write-host $ password现在会打印到引号之前的第一个反引号.是的,它输出反引号,这似乎表明一切都在解析所有错误.哦,脚本中调用的其他函数之一引发了一个我之前从未见过的异常​​(异常调用"AcquireToken"和"4"个参数:"authentication_canceled:用户取消了身份验证"),这表明该参数解析完全以一种新颖而有趣的方式进行.这是写主机$ password产生的:

Result: write-host $password now prints up to the first backtick before the quote. And yes, it outputs the backtick, which seems to show that everything is parsing all wrong. Oh, and one of the other functions called in the script threw an exception I haven't seen before (Exception calling "AcquireToken" with "4" argument(s): "authentication_canceled: User canceled authentication"), which suggests that the param parsing was completely hosed in a new and fascinating way. Here's what write-host $password produced:

xxx<xxxx;xxxxx`


powershell .\makeConfig.ps1 -password "xxx<xxxx`";`"xxxxx`"&`"x.xxxx}xx/xxx" -otherparam 3

结果:write-host $ password现在会打印到引号之前的第一个反引号.第二个参数处于默认状态. (即,与上次结果相同.)从好的方面来说,上一个示例中引发的异常没有出现.

Result: write-host $password now prints up to the first backtick before the quote. And the second parameter is in the default state. (i.e., same result as last time.) On the plus side, the exception thrown in the previous example didn't crop up.

那么,如何从Windows命令行中将任意ASCII字符串传递给powershell字符串参数?或者至少,我如何转义分号,&"号和花括号?

推荐答案

powershell .\makeConfig.ps1 -password "'xxx<xxxx;xxxxx&x.xxxx}xx/xxx'" -otherparam 3

这里

  • outer " double quotes would escape all cmd poisonous characters e.g. <, & etc. excepting " itself and % percentage sign (and ! exclamation mark if delayed expansion is enabled) while
  • inner ' apostrophes (single quotes) would escape all powershell ones (excepting ' apostrophe itself).

但是,上面提到的异常也可以逃脱…… 基于转义字符,定界符和引号文章:

However, exceptions noticed above could be escaped as well… Based on both Escape characters, Delimiters and Quotes articles:

  • in cmd and batch-file
  • in powershell

这篇关于如何在命令行Powershell参数中转义“&"号,分号和花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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