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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/582896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:12:02  来源:igfitidea点击:

Batch Script to Install or Uninstall a .NET Windows Service

.netinstallerservicebatch-filewindows-server-2003

提问by John Rasch

I have no experience writing batch scripts, but I was wondering if there was a way to install a .NET Windows service using installutil.exeusing 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).

我没有编写批处理脚本的经验,但我想知道是否有办法installutil.exe使用这样的脚本安装 .NET Windows 服务,或者卸载该服务(如果它已经安装),最好通过某种确认我实际上会喜欢执行卸载(例如按 y 卸载)。

Here are some details:

以下是一些细节:

  • The .exe for the service is located in the C:\Program Files\Data Servicedirectory
  • 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
  • 服务的 .exe 位于C:\Program Files\Data Service目录中
  • 该脚本应与服务的 .exe 位于同一目录中
  • program.log安装服务后,最好在日志文件中添加一行简单的行(我们称之为,也在此目录中)
  • 机器运行 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.

如果您觉得这可以以更好的方式完成,那么很高兴听到其他建议。我总是可以编写服务安装程序,但这不是优先事项。

采纳答案by VBNight

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.

您可以使用命令行参数(-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 构建安装包可能会更快。

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

回答by Kinze

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

回答by Annagram

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

回答by Jos

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

回答by iKevin

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

我发现使用一个好的安装项目来使用批处理文件来安装应用程序总是更好。有时那是做不到的。有几个项目是在 Windows NT 和早期的 Windows XP 时代编写的,并使用简单的批处理文件进行安装。在那些时候,将批处理文件转换为安装包比简单的调整更麻烦。通过大量搜索,我发现http://ss64.com/nt/是一个非常好的 Windows 批处理文件参考。(随着我们软件技术的进步,不得不写最后一句话,这感觉很奇怪。)

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

无论如何,快乐编码!- 与“语言”无关。

回答by Al W

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

回答by chevhfghfghfgh

@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文件

回答by Richard

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.
  • 使用环境,windows可能没有安装在C:上。但是您可以使用 %WinDir%。
  • 您可以重定向 echo 以附加到文件:

    echo 一条消息>> logfile.txt

  • 在 cmd.exe 中跟踪所有内容并转换所有边缘情况可能具有挑战性,它不是一个丰富的环境。

  • 文档没有一致的地方。但是“cmd”、“if”、“for”、“set”和“call”的帮助(来自命令行)涵盖了大部分可用的语法。
  • 在开始时设置 echo off 以查看执行的命令。

回答by David

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


回答by Oscar Cabrero

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

创建一个扩展名为 .bat 的文件并将其放在文件中

installutil -u c:\YourServiceLocation\Service.exe

installutil -uc:\YourServiceLocation\Service.exe