views:

7422

answers:

6

I have no experience writing batch scripts, but I was wondering if there was a way to install a .NET Windows service using installutil.exe using such a script, or uninstall the service if it is already installed, ideally with some kind of confirmation that I actually would like to perform the uninstall (e.g. press y to uninstall).

Here are some details:

  • The .exe for the service is located in the C:\Program Files\Data Service directory
  • The script should be in the same directory as the .exe for the service
  • It would be nice to add a simple line to a log file (we'll call it program.log, also in this directory) after the service has been installed
  • The machine is running Windows Server 2003 with the .NET Framework installed in the default directory C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

If you feel this could be done in a better way it would be nice to hear other suggestions. I could always write a service installer but that is not a priority.

+4  A: 

You could setup your service exe to support self registration / unregistration using command line arguments (-i -u etc) instead of writing a batch file to do the same thing.

Information on creating Self Installing Services In .NET

http://anotherlab.rajapet.net/2006/06/self-installing-services-in-net.html

http://www.gotnet.biz/WindowsServiceSelfInstaller.ashx

Also adding a Setup Project to your solution and having Visual Studio build an install package might be faster.

How to create a Setup project for a Windows Service in Visual Basic .NET or in Visual Basic 2005

(VB) http://support.microsoft.com/kb/317421

(C#) http://support.microsoft.com/kb/816169

VBNight
A: 

Suggestions:

  • Make use of the environment, Windows may not be installed on C:. But you can use %WinDir%.
  • You can redirect echo to append to a file:

    echo A message >> logfile.txt

  • Keeping track of everything and convering all the edge cases can be challenging in cmd.exe, it is not a rich environment.

  • There is no consistent place for documentation. But help (from the command line) on "cmd", "if", "for", "set" and "call" covers much of avaialble syntax.
  • Set echo off at the start to see the commands as they are executed.
Richard
+1  A: 

i'm not sure why you'd need a batch file for a one liner. this is what i'd use.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i ServiceAssembly.dll

Al W
+3  A: 

This is the batch files I used to install.

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i MyService.exe
echo ---------------------------------------------------
echo Done.
pause

To Uninstall I used the following:

@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Uninstalling MyService...
echo ---------------------------------------------------
InstallUtil /u MyService.exe
echo ---------------------------------------------------
echo Done
Kinze
A: 

I did this with an old fashioned batch file....

Copy the installutil.exe into the same directory as your executable (to make things easier) The following is a generic example of the contents of the batch file necessary: (mine was just names instal.bat)


installutil MyService.exe sc config MyService type= interact type= own sc failure MyService reset= 6000 actions= restart/5000/restart/5000/restart/5000 sc start MyService


For more info on command line options for installutil.exe, do an MSDN search on installutil

To uninstall the service, use a different batch file with the following contents:


installutil MyService.exe /u


David Stratton
A: 

create a file with .bat extension and place this in the file

installutil -u c:\YourServiceLocation\Service.exe

Oscar Cabrero