Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Script to Install or Uninstall a .NET Windows Service

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.

like image 886
John Rasch Avatar asked Feb 24 '09 18:02

John Rasch


2 Answers

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
like image 92
Kinze Avatar answered Nov 05 '22 19:11

Kinze


It is easier to just make self-installing services. Once you implement this, you can either run the service exe directly with the (/i or /u switch), or wrap the call in a batch file if you'd like.

static void Main(string[] args)
{
    if (args.Length > 0)
    {
        //Install service
        if (args[0].Trim().ToLower() == "/i")
        { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/i", Assembly.GetExecutingAssembly().Location }); }

        //Uninstall service                 
        else if (args[0].Trim().ToLower() == "/u")
        { System.Configuration.Install.ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); }
    }
    else
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}
like image 26
Annagram Avatar answered Nov 05 '22 20:11

Annagram