Welcome › Forums › General PowerShell Q&A › How to use escape sequence in powershell command
- This topic has 12 replies, 3 voices, and was last updated 4 months ago by
Senior Moderator.
-
AuthorPosts
-
-
September 2, 2020 at 10:49 am #254060
Hello,
I have one PowerShell command as follows:
powershell.exe script.ps1 -Data @{uname='{}';pass='{}'}
So, here password may contain special character like ~!@#$'%^&. when someone enters a single inverted comma ' then scripts fail as it doesn’t form the exact command. Mostly it seems I need to escape it. Can someone help me to escape it?
Any thoughts or workarounds are appreciated.
Thanks
-
September 2, 2020 at 10:51 am #254066
Escaping in PowerShell is by using the backtick `, which normally comes above the Tab key in the keyboard.
-
September 2, 2020 at 11:24 am #254108
Thanks, kvprasoon for the quick reply.
I have tried that it didn’t seem to work here for an example:powershell.exe script.ps1 -Data @{uname=’newuser’;pass=’secure{BACK-TICK}
‘string’}
Here, the password secure{BACK-TICK}’string didn’t work. is there any way so that I can ignore PowerShell string interpretation inside single inverted commas.
Thanks
-
September 2, 2020 at 12:30 pm #254141
how are you collecting the password? is it via a PowerShell script ?
-
September 2, 2020 at 2:20 pm #254189
Backtick escape won’t work within a single quoted string. You can use ''(single quote twice) to escape a single quote. How you call powershell.exe also matters here. I am pretty sure you cannot pass a hashtable this way using the -File parameter. Once the current shell interprets the parameter, it is passed as a literal string to PowerShell.exe, i.e. the following will be passed.
PowerShell1@{uname='{}';pass='{}'}.ToString() -
September 3, 2020 at 10:55 am #254393
Sorry for the late reply,
kvprasoon: how are you collecting the password? is it via a PowerShell script?
Answer: basically password is collect in python and I am creating a string in python and executes the PowerShell script:
command = powershell.exe script.ps1 -Data @(@{uname=’newuser’;pass=’ss’s’})
AdminOfThings: trying with twice single quote worked but is there any way I can pass the string to Powershell telling to ignore the escape sequences.
-
September 4, 2020 at 10:13 am #254561
You have to apply enough escaping so that when the string arrives at its final destination, it contains what you want it to contain. Depending on the shell from which you are calling, you can signal the parser to stop parsing (–%) (dash dash percent) the string as PowerShell:
PowerShell1powershell.exe script.ps1 --% -Data @(@{uname='newuser';pass='ss's'}) -
September 4, 2020 at 11:08 am #254582
Thanks, AdminOfThings for the quick reply.
I tried the code example given by you, but it didn’t work and thrown error:-
The string is missing the terminator symbol ‘.
Seems like a string is formed completed as it needs ending single quote. And thanks for sharing knowledge about such operator –%. Just for specification, my PowerShell script accepts hashtable input not plain string input for username and password.
Thanks
-
September 4, 2020 at 1:11 pm #254612
So here is a super screwed up way of doing this in cmd shell:
PowerShell1powershell.exe -command "$command = \""powershell.exe .\script.ps1 -Data @{uname='newuser';pass='ss'more's'}\"" -replace \""(?<=pass='.*?)'(?=.*'})\"",\""''\""; & (-split $command)[0] ($command -split ' ',2)[1]"As you can see, CMD shell adds it own escape requirements. Inner double quotes have to be backslash escaped in CMD. Then inner quotes (ones that don’t close the outer quotes) require escaping at the PowerShell layer. Since you are using Python, this won’t solve your problem. However, the Python environment may have its own escape requirements and string replacement mechanisms that can be utilized before ultimately sending the command string to PowerShell.exe.
-
September 4, 2020 at 6:14 pm #254678
The double single quote escapes a single quote when it is interpreted by PowerShell. Your job will be to make sure Python hands it off to PowerShell.exe with PowerShell’s required escape characters available.
-
September 5, 2020 at 2:05 pm #254771
you can make your script accept base64 encoded string as password then convert it to plaintext where required. So you can pass base64 encoded string without any special characters.
-
September 8, 2020 at 2:06 pm #255356
-
September 12, 2020 at 5:58 am #256085
Hi kvprasoon, I have implemented as per your suggestion i.e transfer base64 encoded string to Powershell and it worked well but there is another command inside PowerShell script which sets credentials to windows service with help of NSSM as per the command shown below:
& .\nssm.exe set $ServiceName ObjectName $username $password
Here, while transferring password containing special characters is decoded(base64) in PowerShell and the required password is fetched in PowerShell. Now bit confused about passing password(with special characters) to nssm command. Any thoughts/approaches are appreciated.
Thanks
-
-
AuthorPosts
- The topic ‘How to use escape sequence in powershell command’ is closed to new replies.