I tried passing credentials to NSSM for setting up log-on credentials for windows service via PowerShell in which credentials included special charaters. Now these credentials containing special charaters are creating issue.
Special charaters include space, inverted comma, double inverted comma, etc
Exact command I tried - .\nssm.exe set $ServiceName objectname $Username $Password
Now, the $Passwordvariable contains text’ss""data"string.
I more thing I already tried was to wrap variables in inverted comma but I was confused in creating escape sequence function: .\nssm.exe set “”“$ServiceName”“” objectname “”“$Username”“” “”“$Password”“”
Not exactly the same but it seems like a different question i.e passing special characters to some executable while that was about passing specials characters to PowerShell script from any other script(not specifically ps1). And thanks for your suggestion to pass the encoded string.
Also, I have also tried the different approaches along with wrapping credentials in double-quotes and escaping the double inverted comma. Here are some test-casxes as follows:
Like this? Using a here string and echoargs (this forum can’t print less than and greater than signs that echoargs outputs). You don’t say how you set the variable. The backslashes are definitely required to pass the double quotes to an external program.
[quote quote=259228]Like this? Using a here string and echoargs (this forum can’t print less than and greater than signs that echoargs outputs). You don’t say how you set the variable. The backslashes are definitely required to pass the double quotes to an external program.
One thing I am also facing the same error on this side
I understand that I need to escape double inverted comma but if I have already have on back-slash in my password then I need to use explicitly escape that also(still no issue). But now if I have one back-slash which isolated then there is no need for escaping it. By the above finding I am not able create generalized function for escaping the characters.
On more thing I am passing password string to powershell via command arguments. So “here string” is not useful.
Hmm … dont be offended, but I have to ask, you have you tried testing strictly from the command line? Is it possible the nssm.exe does not like the chars you have chosen and the issue does not lie with PowerShell?
You concern is genuine, but I have verified this by running following command manually on powershell and it works are expected. And nssm.exe allows such characters if some how we can transfer those! But only thing I need to mention is that while using powershell terminal I need to escape characters manually i.e
To pass one double inverted comma - I need to pass two double inverted comma.
For string containing backward slash and double inverted comma - I need to pass two backward slash and two double inverted comma.(This rule make whole issue more confusing)
.\nssm.exe set "some service name" objectname --% "some""data'string" "some'string"
By the above command it was working as expected.
Now, I tried other approach as per you suggestion to try Invoke-Expression and Here-string
Code cannot be printed on this portal due to escape character issue, I am attaching an image for reference Image
Now, main issue is that I am not able to create a general function which adds the escape characters where ever necessary.
to change 1 double inverted comma to 2 double inverted comma
data"string
data""string
to change 1 backward slash adjoin with 1 double inverted comma --> 2 backward slash
data\"string
data\\""string
also is field contains 2 backward slash adjoin with 1 double inverted comma --> 4 backward slash
data\\"string
data\\\\""string
Now, some confusing point, i.e if I have 1 backward slash without adjoining any double inverted commas then it will consider that as literal backward slash only
data"string\extra
data""string\extra
Any help around creating such function would be appreciated.
Thanks,