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 u
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".
Suggestions:
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.
@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
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);
}
}