1

Does NAnt have the ability to install or uninstall a windows service, using the InstallUtil utility or whatever else?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Brian Mains
  • 50,520
  • 35
  • 148
  • 257

4 Answers4

3

You can call Nant's exec task to call InstallUtil and can pass parameters to install or uninstall a service easily

 <target name="install-service">
    <exec program="${framework::get-framework-directory('net-2.0')}\InstallUtil.exe">
      <arg value="-i" />
      <arg value="/name=V1" />
      <arg value="C:\Service\SomeService.exe" />      
    </exec>
  </target>
caltuntas
  • 10,747
  • 7
  • 34
  • 39
  • 1
    instead of hard-linking to the path of installutil.exe, you should use the nant functions to get the path of the framework first: framework::get-framework-directory('net-2.0'): http://nant.sourceforge.net/release/latest/help/functions/framework.get-framework-directory(System.String).html – Garo Yeriazarian Aug 05 '10 at 22:38
1

Nant or MSBuild? What's the problem with just running installutil yourself - that's what you'd do in MSBuild. (In general, builds dont do the installs for things like this as rule as typically your build should be able to run on a random build server).

Another option, which would take installutil out of the equation is adding a self-install option to your service like this (have a search for more by looking for self install windows service)

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • NAnt, sorry for the confusion with the tags. – Brian Mains Aug 06 '10 at 00:30
  • I have a more complicated process that runs outside of the service, so I would like to leverage everything with NAnt. – Brian Mains Aug 06 '10 at 00:31
  • @Brian: Cool (I subscribe to the msbuild tag - Sticking on a build-automation tag. In general installutil isnt a bad approach (though [the bootstrapping issues with it](http://robmensching.com/blog/posts/2007/4/19/Managed-Code-CustomActions-no-support-on-the-way-and-heres) are definitely important to be aware of – Ruben Bartelink Aug 06 '10 at 07:43
1

If your service can be installed at different places, you can also uninstall it through its name using SC.EXE, as follows:

<property name="serviceName" value="Name of the service"/>
<exec program="sc" failonerror="false" verbose="true" if="${service::is-installed(serviceName,'.')}">
 <arg value="delete"/>
 <arg value="${serviceName}"/>
</exec>
jcmeyrignac
  • 129
  • 3
  • service::is-installed required nant-contrib - http://nantcontrib.sourceforge.net/release/0.85/help/functions/service.is-installed.html – Castrohenge Jul 17 '14 at 11:20
0

If you use the TopShelf Project in your application to host your services, you can get command-line based tools for installing / removing the services without needing InstallUtil.

ServiceName.exe service install ServiceName.exe service uninstall

And you can run the service directly and get a nice console window that you can CTRL+C to stop. You can integrate this directly into nant or msbuild by executing the program.

Garo Yeriazarian
  • 2,533
  • 18
  • 29
  • I have a more complicated process than that, which is why I am using NAnt to consolidate... – Brian Mains Aug 06 '10 at 00:32
  • If you just need something that will call InstallUtil on your services, then I recommend going with mcaaltuntas's answer. You can even wrap it in a in nant that will iterate on all the .exe's or all the services and install it all for you. – Garo Yeriazarian Aug 06 '10 at 05:14