I am using the non-sucky service manager nssm, and for the purposes of this exercise, I put a copy in the root folder on my C: drive.
I am trying to write an update script to run in powershell, that can be invoked by my installer to add a new program argument, or AppParameters value in the NSSM verbiage, if it isn’t already there.
what happens is only the first character of the leading argument gets set, when I follow the logic to first get the existing value, split it into an array, and add the extra array value.
I’ve trimmed this down to the bare bones for demonstration purposes, so if you put nssm.exe in C: or you edit to where your instance is installed, and run the script below, hopefully you see what I see which is getting, adding, and setting doesn’t work, however if I call my function with a string array of the original values, it does.
I have iterated over this for a couple of days.
$serviceName = 'someservice'
$parameterName = 'AppParameters'
$nssm = "C:nssm.exe"
function Modify-Service {
Param (
[String]$serviceName,
[String]$parameter,
[String[]]$parameters
)
#show the type
$parameters.GetType()
#list out the array
$parameters
& $nssm set $serviceName $parameter "$parameters"
}
#Set up the bare minimum to demonstrate the issue
& $nssm install $serviceName $nssm
& $nssm set $serviceName $parameterName "headless mpxsreqport=22744 mpxspubport=22745 author"
& $nssm get $serviceName $parameterName
#returns headless mpxsreqport=22744 mpxspubport=22745 author
$parameterValue = (& $nssm get $serviceName $parameterName)
$paramList = $parameterValue[0] -split "s+"
$paramList += "noDialogs"
[String[]]$orig = "headless", "mpxsreqport=22744", "mpxspubport=22745", "author"
#As I understand it, both $orig and $paramList show as String Arrays
#and in the Modify-Service, the "$parameters" construct joins the array into a string which is fed in as a single argument
#For some reason the result of this operation is just the leading 'h' from headless
Modify-Service -serviceName $serviceName -parameter $parameterName -parameters $paramList
& $nssm get $serviceName $parameterName
#returns h
#Whereas this seems to work
Modify-Service -serviceName $serviceName -parameter $parameterName -parameters $orig
& $nssm get $serviceName $parameterName
#returns headless mpxsreqport=22744 mpxspubport=22745 author
#Clean-Up
& $nssm remove $serviceName confirm
user3038022 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.