Pass special charaters from powershell to nssm.exe

Hii,

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”“”

Any approaches/idea are appreciated.

Thanks,

Dave

is it the same as https://powershell.org/forums/topic/how-to-use-escape-sequence-in-powershell-command/ ?

Hello, kvprasoon

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:

$Password = $pass -replace '\"', '\"'
.\nssm.exe set """$ServiceName""" objectname """$Username""" """$Password"""

Below are some test scenarios:

Input password $Password value Output/Password it got in log-on-credentials
secret"123 secret\"123 secret"123
secret""123 secret\"\"123 secret""123
secret\"123 secret\\"123 secret\123
secret\""123 secret\\"\"123 secret\"123
secret\\""123 secret\\\"\" secret\""123
Thanks

Dave

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.

$password = @'
text'ss\"\"data"string
'@

echoargs $password

Arg 0 is text'ss""datastring

The forum keeps saying:

"There has been a critical error on your website.

Learn more about debugging in WordPress."

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

PowerShell
<textarea class="urvanov-syntax-highlighter-plain print-no" style="-moz-tab-size: 4; font-size: 14px !important; line-height: 18px !important; z-index: 0; opacity: 0;" readonly="readonly" data-settings="dblclick">$password = @' text'ss\"\"data"string '@

echoargs $password

Arg 0 is text’ss""datastring</textarea>

1
2
3
4
5
6
7
$password = @'
text'ss\"\"data"string
'@
echoargs $password
Arg 0 is text'ss""datastring
The forum keeps saying:

“There has been a critical error on your website.

Learn more about debugging in WordPress.”

[/quote]
Thanks for reply,

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.

Thanks,

Maharshi

I tried to submit this before and also got ““There has been a critical error on your website.” here goes again.

I was able to do something similar using Invoke-Expression … I was not using the same chars in the PW as you, but maybe worth a try.

$cmdToExec = “c:\Path\To\nssm.exe set $ServiceName objectname $Username $Password”

Invoke-Expression -Command $cmdToExec

Hi TonyD05,
I tried your suggestion, but it doesn’t work when password contains special characters else it was working fine.

Still I am exploring options if I can send command containing special characters and may be set flag to stop parsing escape characters after that.

Thanks,

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?

Hi TonyD05,

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

  1. To pass one double inverted comma - I need to pass two double inverted comma.
  2. 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

  1. Code cannot be printed on this portal due to escape character issue, I am attaching an image for reference Image
  2. Now, main issue is that I am not able to create a general function which adds the escape characters where ever necessary.
    1. to change 1 double inverted comma to 2 double inverted comma
      data"string data""string
    2. to change 1 backward slash adjoin with 1 double inverted comma --> 2 backward slash
      data\"string data\\""string
    3. also is field contains 2 backward slash adjoin with 1 double inverted comma --> 4 backward slash
      data\\"string data\\\\""string
    4. 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,