Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

parameters - How to escape ampersands, semicolons, and curly braces in command-line powershell params?

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.

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.)

Here's what I've tried:

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

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

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

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.

So how do I pass in arbitrary ASCII strings to powershell string params from the Windows command line? Or at least, how do I escape semicolons, ampersands, and curly braces?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
powershell .makeConfig.ps1 -password "'xxx<xxxx;xxxxx&x.xxxx}xx/xxx'" -otherparam 3

Here

  • 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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...