Installing Windows Service with batch file?
Asked Answered
T

4

11

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?

Thomasinathomasine answered 28/9, 2012 at 15:45 Comment(1)
Just guessing, but what about .\My.WindowsService.exe?Derogative
T
14

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.
Thomasinathomasine answered 9/11, 2012 at 8:16 Comment(2)
"%~dp0" - The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself. Check this answerParaphrastic
Why bother set DOTNETFX2 and set PATH when you've hardcoded the path to InstallUtil as C:\Windows\Microsoft.NET\Framework\v4.0.30319\ ?Erny
A
6

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:

Aceydeucy answered 28/9, 2012 at 15:52 Comment(0)
N
2

It is too old but was useful for me... I made some changes.

My service starts manually so, in the batch I included the start command and then open the console for checking the Installed Service.

@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 MyService Win Service...
echo ---------------------------------------------------
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "%~dp0MyService.exe"
net start MyService
services.msc
echo ---------------------------------------------------
pause
echo Done.
Nominative answered 30/7, 2013 at 15:57 Comment(0)
P
1

Examples of install and uninstall


@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 Instalar Servicio......
echo ---------------------------------------------------
InstallUtil /i "%~dp0WindowsService1.exe"
echo ---------------------------------------------------
pause
echo Done.



@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 Desintalar Servicio...
echo ---------------------------------------------------
InstallUtil /u "%~dp0WindowsService1.exe"
echo ---------------------------------------------------
pause
echo Done.
Protozoon answered 17/1, 2019 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.