Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing Windows Service with batch file?

I have the following in a bat file :

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing IEPPAMS Win Service...
echo ---------------------------------------------------
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil My.WindowsService.exe
echo ---------------------------------------------------
pause
echo Done.

The problem is that it even if the bat file is located in the same folder as the My.WindowsService.exe it will try to look for it in C:\Windows\System32.....

How do I solve this?

like image 389
Banshee Avatar asked Sep 28 '12 15:09

Banshee


People also ask

How do I start a Windows Service from a batch file?

The net start command is used to start the services. Also if the service has spaces in it's name, it must be enclosed in double quotes. Once you have entered the text, save and close the file. When you want to start up the program, all you need to do is double click on the batch file and it will run.


2 Answers

This is how it is solved :

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing IEPPAMS Win Service...
echo ---------------------------------------------------
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil "%~dp0My.WindowsService.exe"
echo ---------------------------------------------------
pause
echo Done.
like image 124
Banshee Avatar answered Oct 15 '22 01:10

Banshee


According to several articles I've found, passing an absolute path to your service is what you want. For example:

{...Path_To_.NET_Framework...}\InstallUtil C:\MyFolder\My.WindowsService.exe

You can grab your current directory with something like this in your batch file, if you want a dynamically generated path:

set CURDIR=%CD%
{...Path_To_.NET_Framework...}\InstallUtil %CURDIR%\My.WindowsService.exe

References:

  • How to Install Windows Service using command prompt
  • Install Windows Service
like image 43
Jonah Bishop Avatar answered Oct 15 '22 01:10

Jonah Bishop