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?
Use the -Replace Operator to Escape Special Characters in PowerShell. The -Replace operator replaces texts or characters in PowerShell. You can use it to remove texts or characters from the string. The -Replace operator requires two arguments: the string to find and the string to replace from the given input.
The escape character in PS is the "back-tic" = `.
The PowerShell escape character is the backtick "`" character. This applies whether you are running PowerShell statements interactively, or running PowerShell scripts. I have not determined why, but the pound sign character "#" does not need to escaped as part of a hard coded Distinguished Name in PowerShell.
Can you give us a bit more details about your code? (To escape the backslash you need to use double backslash : "\\" instead of "\".)
powershell .\makeConfig.ps1 -password "'xxx<xxxx;xxxxx&x.xxxx}xx/xxx'" -otherparam 3
Here
"
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 '
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:
cmd
and batch-file
I'm running firebase
using a powershell script. I solved the problem by using triple double-quotes like this: firebase deploy --token "$firebaseToken" --project "$firebaseProject" --message """$releaseMessage"""
. The URL is in the $releaseMessage
, and the ampersand doesn't cause a problem anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With