13

What is the best way to install a windows service written in C# (in the standard way) on a remote machine, where I need to provide the username and password it should run as?

I am going to run it from MSBuild as part of integration tests.

EDIT: I don't have an msi and I don't want to create one.

Grzenio
  • 35,875
  • 47
  • 158
  • 240

4 Answers4

28

You can use the SC command.

sc.exe \\remotecomputer create newservice binpath= C:\Windows\System32\Newserv.exe start= auto obj= DOMAIN\username password= pwd

(Note the spaces after the equals signs are important)

Creates a service entry in the registry and Service Database.
SYNTAX: 
sc create [service name] [binPath= ] <option1> <option2>...
CREATE OPTIONS:
NOTE: The option name includes the equal sign.
 type= <own|share|interact|kernel|filesys|rec>
       (default = own)
 start= <boot|system|auto|demand|disabled>
       (default = demand)
 error= <normal|severe|critical|ignore>
       (default = normal)
 binPath= <BinaryPathName>
 group= <LoadOrderGroup>
 tag= <yes|no>
 depend= <Dependencies(separated by / (forward slash))>
 obj= <AccountName|ObjectName>
       (default = LocalSystem)
 DisplayName= <display name>
 password= <password> 
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • Looks really promising, but do I have to pay for it? – Grzenio Jul 21 '09 at 13:28
  • Its included in Windows, possibly in the Resource Kit, start a command prompt and type sc to see if its installed. – Patrick McDonald Jul 21 '09 at 13:47
  • 1
    Just a note - if running this from powershell it is sc.exe not sc. sc is an alias for Set-Content in powershell. Just a little gotcha – Crab Bucket Jul 08 '15 at 13:09
  • Be careful with passing complex passwords with special characters as even double quotes around the password won't save you, https://stackoverflow.com/questions/10296162/escaping-special-characters-in-cmd – duct_tape_coder Apr 10 '20 at 18:45
1

Installutil called from WMI invoked from Powershell is one way to go.

Raj More
  • 47,048
  • 33
  • 131
  • 198
0

It might be worth you checking out a utility I wrote which lets you install/uninstall/start/stop windows services on a local or remote machine. If you have a feature you need added let me know (comment/github issues or PM and I'll happily oblige).

ServiceConfigurator on GitHub

As @Raj More suggested, it uses WMI invoked from .NET.

Echilon
  • 10,064
  • 33
  • 131
  • 217
0

We used to use PsExec to do everything on remote machine. At this time I saw naother solution called PowerShell Remoting, but haven't tried myself.

Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105