How can I automate a Topshelf interactive service

2019-04-14 12:53发布

问题:

I have a couple Topshelf services that run under a specific service account. If I install the services manually from a command line, I can use the --interactive flag, and a dialog box comes up allowing me to enter the username and password. I'd like to be able to automate this through a Powershell script.

Does anyone know how to do this? (Not concerned about Powershell specifically, but with how can I provide the username and password in any installation script.)

As Travis mentioned, I took a look at the command line options. Almost working for me, but now I have one escaping issue. To install, you can type, e.g., MyService.exe install -username:Foo -password:Bar.

However, I have to provide both the domain and username for the username option (I know this from doing the --interactive route): MyService.exe install -username:mydomain\$myusername -password:Bar

I cannot find a way to escape this that works! Sorry -- my question has morphed into something else, might need to mark it answered and open a different one.

回答1:

Travis pointed me in the right direction with the command line options. I had one more problem with the service account username I had, which was prefixed with a "$": domain\$myuser. I could not find a way to escape it so the "install" command would accept it.

We created a similar username "myuser" (without the $). Now this works just fine:

MyService.exe install -username:domain\myuser -password:Bar 


回答2:

The username and password are configuration in the app.config for services in my setting. This is used in the service setup block in the RunAs.

Additionally, there are other command line options for Topshelf. I don't know if the documentation is 100% up to date but it's a good place to start.



回答3:

For future readers:

See this url:

https://kristofmattei.be/2015/01/15/topshelf-install-powershell-get-credentials/

Here is the important quote from the url above

Start Quote

So the best version is:

$credentialsOfCurrentUser = Get-Credential -Message "Please enter your username & password for the service installs"
$networkCredentials = $credentialsOfCurrentUser.GetNetworkCredential();
$username = $credentialsOfCurrentUser.UserName
$password = $networkCredentials.Password

Now that we have those variables we can pass them on to the install of the Topshelf exe:


. $pathToServiceExe install -username `"$username`" -password `"$password`" --autostart


Notice the backticks (`) to ensure the double quotes are escaped.

End Quote



标签: topshelf