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?
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.
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.
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:
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.
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.
AdminOfThings I got your point, It seems like I need to escape the required string in python only and pass it to the PowerShell script by this I will achieve the required string with escaped special characters.
And in this case, as I have a string written in single quotes I only need to escape a single quote (i.e by using it twice). Is my interpretation is correct?
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.
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.
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.