Batch Script to Install or Uninstall a .NET Window

2019-03-09 03:20发布

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.

10条回答
姐就是有狂的资本
2楼-- · 2019-03-09 03:45

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

查看更多
We Are One
3楼-- · 2019-03-09 03:47

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
查看更多
不美不萌又怎样
4楼-- · 2019-03-09 03:51

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);
    }
}
查看更多
贼婆χ
5楼-- · 2019-03-09 03:55

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

installutil -u c:\YourServiceLocation\Service.exe

查看更多
\"骚年 ilove
6楼-- · 2019-03-09 03:57

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".

查看更多
迷人小祖宗
7楼-- · 2019-03-09 03:58

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

查看更多
登录 后发表回答