简体   繁体   English

用于安装或卸载 .NET Windows 服务的批处理脚本

[英]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 (eg press y to uninstall).我没有编写批处理脚本的经验,但我想知道是否有办法使用installutil.exe安装 .NET Windows 服务,或者如果它已经安装,则卸载该服务,理想情况下通过某种确认我实际上想要执行卸载(例如按 y 卸载)。

Here are some details:以下是一些细节:

  • The.exe for the service is located in the C:\Program Files\Data Service directory该服务的.exe 位于C:\Program Files\Data Service目录中
  • The script should be in the same directory as the.exe for the service该脚本应与服务的 .exe 位于同一目录中
  • 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安装服务后,最好在日志文件(我们将其称为program.log ,也在此目录中)添加一个简单的行
  • The machine is running Windows Server 2003 with the .NET Framework installed in the default directory C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727机器运行 Windows Server 2003,.NET Framework 安装在默认目录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.我总是可以编写服务安装程序,但这不是优先事项。

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

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.实现此功能后,您可以使用(/i 或 /u 开关)直接运行服务 exe,或者如果您愿意,可以将调用包装在批处理文件中。

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);
    }
}

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.您可以使用命令行 arguments(-i -u 等)设置您的服务 exe 以支持自我注册/注销,而不是编写批处理文件来执行相同的操作。

Information on creating Self Installing Services In .NET在 .NET 中创建自安装服务的信息

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

http://www.gotnet.biz/WindowsServiceSelfInstaller.ashx 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.此外,将安装项目添加到您的解决方案并让 Visual Studio 构建安装 package 可能会更快。

How to create a Setup project for a Windows Service in Visual Basic .NET or in Visual Basic 2005如何在 Visual Basic .NET 或 Visual Basic 2005 中为 Windows 服务创建安装项目

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

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

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

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.工作中的几个项目是在 Windows NT 和早期 Windows XP 时代编写的,并使用简单的批处理文件进行安装。 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.通过大量搜索,我发现http://ss64.com/nt/是一个非常好的 Windows 批处理文件参考。 (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". - 无论“语言”如何。

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 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i ServiceAssembly.dll

@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以管理员身份运行此 bat 文件

Suggestions:建议:

  • Make use of the environment, Windows may not be installed on C:.利用环境,C:上可能安装不了Windows。 But you can use %WinDir%.但是你可以使用 %WinDir%。
  • You can redirect echo to append to a file:您可以将回显重定向到 append 到文件:

    echo A message >> logfile.txt回显消息>> logfile.txt

  • Keeping track of everything and convering all the edge cases can be challenging in cmd.exe, it is not a rich environment.在 cmd.exe 中跟踪所有内容并转换所有边缘情况可能具有挑战性,它不是一个丰富的环境。

  • 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.但是关于“cmd”、“if”、“for”、“set”和“call”的帮助(来自命令行)涵盖了大部分可用的语法。
  • Set echo off at the start to see the commands as they are executed.在开始时设置 echo off 以查看执行的命令。

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.exe 复制到与可执行文件相同的目录中(为了使事情更容易)以下是必要的批处理文件内容的通用示例:(我的只是名称 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 .有关 installutil.exe 命令行选项的更多信息,请参见此处

To uninstall the service, use a different batch file with the following contents:要卸载该服务,请使用具有以下内容的不同批处理文件:


installutil MyService.exe /u

create a file with.bat extension and place this in the file创建一个扩展名为 .bat 的文件并将其放入文件中

installutil -u c:\YourServiceLocation\Service.exe installutil -u c:\YourServiceLocation\Service.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM