Pass special charaters from powershell to nssm.exe

Welcome Forums General PowerShell Q&A Pass special charaters from powershell to nssm.exe

Viewing 7 reply threads
  • Author
    Posts
    • #259072
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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

    • #259084
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP
    • #259171
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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:

      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

      • This reply was modified 4 weeks ago by grokkit. Reason: code formatting - please read the guide
    • #259441
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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.

      The forum keeps saying:

      “There has been a critical error on your website.

      Learn more about debugging in WordPress.”

      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

    • #259795
      Participant
      Topics: 7
      Replies: 137
      Points: 668
      Helping Hand
      Rank: Major Contributor

      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

    • #260388
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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,

    • #260403
      Participant
      Topics: 7
      Replies: 137
      Points: 668
      Helping Hand
      Rank: Major Contributor

      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?

    • #260670
      Participant
      Topics: 4
      Replies: 13
      Points: 62
      Rank: Member

      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,

Viewing 7 reply threads
  • You must be logged in to reply to this topic.