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).
我没有编写批处理脚本的经验,但我想知道是否有办法使用这样的脚本使用installutil.exe安装.NET Windows服务,或者卸载服务(如果已安装),理想情况下需要某种确认我实际上想执行卸载(例如按y卸载)。
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
该服务的.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.
如果你觉得这可以以更好的方式完成,那么听听其他建议会很高兴。我总是可以编写服务安装程序,但这不是优先事项。
10 个解决方案
#1
13
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 etc)设置服务exe以支持自我注册/取消注册,而不是编写批处理文件来执行相同的操作。
Information on creating Self Installing Services In .NET
有关在.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.
同时向解决方案添加安装项目并让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服务创建安装项目
#2
30
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
#3
23
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);
}
}
#4
6
This is the one I use. I found it and use it. Thanx to the creator..
这是我使用的那个。我发现并使用它。 Thanx给创作者..
@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
#5
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.)
我发现使用一个好的安装项目来使用批处理文件来安装应用程序总是更好。有时候,这是无法做到的。工作中的几个项目是在Windows NT和早期Windows XP时代编写的,并使用简单的批处理文件进行安装。在这段时间内,将批处理文件转换为安装包装比简单的调整更麻烦。通过大量搜索,我发现http://ss64.com/nt/是一个非常好的Windows批处理文件参考。 (我们在软件技术方面取得的所有进步,只是写下最后一句话,这感觉很奇怪。)
Anyway, Happy Coding! - regardless of the "language".
无论如何,快乐编码! - 无论“语言”如何。
#6
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
C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ InstallUtil.exe / i ServiceAssembly.dll
#7
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以附加到文件:
echo A message >> logfile.txt
echo一条消息>> 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.
- Set echo off at the start to see the commands as they are executed.
利用环境,Windows可能无法安装在C:上。但是你可以使用%WinDir%。
文档没有一致的位置。但是对于“cmd”,“if”,“for”,“set”和“call”的帮助(来自命令行)涵盖了很多可用的语法。
在开始时设置echo off以在执行时查看命令。
#8
0
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
#9
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.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
#10
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
以管理员身份运行此bat文件
#1
13
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 etc)设置服务exe以支持自我注册/取消注册,而不是编写批处理文件来执行相同的操作。
Information on creating Self Installing Services In .NET
有关在.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.
同时向解决方案添加安装项目并让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服务创建安装项目
#2
30
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
#3
23
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);
}
}
#4
6
This is the one I use. I found it and use it. Thanx to the creator..
这是我使用的那个。我发现并使用它。 Thanx给创作者..
@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
#5
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.)
我发现使用一个好的安装项目来使用批处理文件来安装应用程序总是更好。有时候,这是无法做到的。工作中的几个项目是在Windows NT和早期Windows XP时代编写的,并使用简单的批处理文件进行安装。在这段时间内,将批处理文件转换为安装包装比简单的调整更麻烦。通过大量搜索,我发现http://ss64.com/nt/是一个非常好的Windows批处理文件参考。 (我们在软件技术方面取得的所有进步,只是写下最后一句话,这感觉很奇怪。)
Anyway, Happy Coding! - regardless of the "language".
无论如何,快乐编码! - 无论“语言”如何。
#6
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
C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ InstallUtil.exe / i ServiceAssembly.dll
#7
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以附加到文件:
echo A message >> logfile.txt
echo一条消息>> 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.
- Set echo off at the start to see the commands as they are executed.
利用环境,Windows可能无法安装在C:上。但是你可以使用%WinDir%。
文档没有一致的位置。但是对于“cmd”,“if”,“for”,“set”和“call”的帮助(来自命令行)涵盖了很多可用的语法。
在开始时设置echo off以在执行时查看命令。
#8
0
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
#9
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.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
#10
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
以管理员身份运行此bat文件