Batch Script to Install or Uninstall a .NET Windows Service
Asked Answered
P

10

19

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.

Platonism answered 24/2, 2009 at 18:16 Comment(0)
C
15

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

Combatant answered 24/2, 2009 at 18:22 Comment(2)
Note that Microsoft removed the Setup project type in Visual Studio 2012.Retainer
all links but the vb one are dead.Dominicadominical
A
32

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
Assiduous answered 24/2, 2009 at 18:30 Comment(2)
When i run the batch file in administrator mode the PATH is referring to C:\Windows\system32\service.exe and not the path the file actually exists, why?Riemann
@Riemann this post was old because of running as administrator role when installing windows service since windows vista. I suggest (setlocal enableextensions) and (cd /d "%~dp0") on the top of each file.Done
S
26

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);
    }
}
Stamps answered 10/2, 2011 at 18:57 Comment(2)
This suggestion is a lot easier than having to maintain setup projects or batch scripts.Mongolia
I'm trying to write Console.WriteLine('') statement thile installing and uninstalling but it's not showing. Any idea why would that be happening ?Exorcist
C
15

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

Combatant answered 24/2, 2009 at 18:22 Comment(2)
Note that Microsoft removed the Setup project type in Visual Studio 2012.Retainer
all links but the vb one are dead.Dominicadominical
D
6

This is the one I use. I found it and use it. Thanx to the creator..

@echo off

SET PROG="YourServiceHere.exe"
SET FIRSTPART=%WINDIR%"\Microsoft.NET\Framework\v"
SET SECONDPART="\InstallUtil.exe"
SET DOTNETVER=2.0.50727
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
SET DOTNETVER=1.1.4322
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
SET DOTNETVER=1.0.3705
  IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install
GOTO fail
:install
  ECHO Found .NET Framework version %DOTNETVER%
  ECHO Installing service %PROG%
  %FIRSTPART%%DOTNETVER%%SECONDPART% %PROG%
  GOTO end
:fail
  echo FAILURE -- Could not find .NET Framework install
:param_error
  echo USAGE: installNETservie.bat [install type (I or U)] [application (.exe)]
:end
  ECHO DONE!!!
  Pause
Distribute answered 22/12, 2010 at 8:13 Comment(0)
P
4

I have found that it is always better to use a good install project that to use batch files for installing an app. There are times though that that can't be done. Several projects at work were written in the days of Windows NT and early Windows XP and use simple batch files for installation. During those times, converting the batch file to an install packed is more trouble than a simple tweak. Through much searching, I have found that http://ss64.com/nt/ is a very good Windows batch file reference. (It just feels strange, with all our advancement in software technolgies, to have to write that last sentence.)

Anyway, Happy Coding! - regardless of the "language".

Pleach answered 26/9, 2012 at 21:20 Comment(0)
C
2

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

Certification answered 24/2, 2009 at 18:23 Comment(0)
E
0

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.
Everybody answered 24/2, 2009 at 18:22 Comment(0)
T
0

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, see here.

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


installutil MyService.exe /u

Tricrotic answered 24/2, 2009 at 18:31 Comment(0)
W
0

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

installutil -u c:\YourServiceLocation\Service.exe

Want answered 24/2, 2009 at 18:36 Comment(0)
S
0
@echo off

SET PROG="c:\YourServiceLocation\Service.exe" SET FIRSTPART=%WINDIR%"\Microsoft.NET\Framework\v" SET SECONDPART="\InstallUtil.exe" SET DOTNETVER=4.0.30319 IF EXIST %FIRSTPART%%DOTNETVER%%SECONDPART% GOTO install

GOTO fail :install ECHO Found .NET Framework version %DOTNETVER% ECHO Installing service %PROG% %FIRSTPART%%DOTNETVER%%SECONDPART% %PROG% GOTO end :fail echo FAILURE -- Could not find .NET Framework install :param_error echo USAGE: installNETservie.bat [install type (I or U)] [application (.exe)] :end ECHO DONE!!! Pause

run this bat file as administrator

Stannite answered 30/5, 2014 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.