How To Install Windows Service Using Installutil Exe
In this commodity I'll testify how to install and configure a Windows Service, query its status and properties, and and then how to uninstall information technology. I'll be using the command line utilities installutil.exe and sc.exe to install and configure the service.
Add together a service installer class to your Windows Service project
In order to employ installutil.exe to install your Windows Service, you need to add a service installer form. Otherwise when you endeavour to install, you'll get the following error:
No public installers with the RunInstallerAttribute.Yes attribute could be establish in the C:\Services\TestService.exe associates.
To create a service installer class:
- Open up your service class file in design mode in Visual Studio.
- Right-click > click Add Installer.
After you click Add Installer, it volition create the service installer class and open up information technology in design mode.
At a bare minimum, you need to set the ServiceName and Account properties.
- Click serviceInstaller1 to bring upward its backdrop.
- Specify the ServiceName belongings.
Side by side, set the Account holding. This property is the account that your service runs as, and so choose whatever makes sense in your example (if y'all're non sure, inquire a security person).
- Click serviceProcessInstaller1 to bring up its properties.
- Ready the Account to whatever value is appropriate in your situation.
Install the service and configure it
The post-obit batch file installs TestService.exe by using installutil.exe, configures the service with sc.exe, and starts the service using cyberspace start:
Code linguistic communication: plaintext ( plaintext )
@ECHO OFF REM Get log file proper noun with timestamp for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (prepare ts=%%a) fix LogName="C:\logs\installTestService%ts:~0,eight%%ts:~8,four%%ts:~12,two%.log" REM Install service set servicePath="C:\Services\TestService.exe" "C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% >> %LogName% REM Add dependency on SQL Server sc config TestService depend=MSSQL$SQLEXPRESS >> %LogName% REM Make get-go automatically sc config TestService get-go= car >> %LogName% REM On crash, restart subsequently 1 minute sc failure TestService deportment= restart/60000/restart/60000// reset= 86400 >> %LogName% REM Starting time the service internet start TestService >> %LogName%
Running this installs the service and outputs to a timestamped log file (ex: C:\logs\installTestService20210205074016.log).
Note: Execute this past using Run as Ambassador.
It configures the service with the following properties:
- Depends on service MSSQL$SQLEXPRESS (SQL Server Express). This ways it won't run unless SQL Server Express is running. When SQL Server Express restarts, it also restarts TestService.
- Starts automatically.
- Restarts later i infinitesimal if the service crashes. Annotation: This is simply configuring it to automobile-restart twice. Later 2 failures, it won't attempt to car-restart. It resets the failure counter after 1 twenty-four hour period.
Query the service properties and status
Y'all can look at the service properties and condition either by looking in Services, or by querying the properties from the control line by using sc.exe.
Execute the following to get the service properties and status:
Code language: plaintext ( plaintext )
sc qc TestService
This shows that the service is running and it'south using the configuration specified in the batch install script (from the previous department):
Code language: plaintext ( plaintext )
[SC] QueryServiceConfig SUCCESS SERVICE_NAME: TestService Blazon : x WIN32_OWN_PROCESS START_TYPE : ii AUTO_START ERROR_CONTROL : 1 NORMAL BINARY_PATH_NAME : "C:\Services\TestService.exe" LOAD_ORDER_GROUP : TAG : 0 DISPLAY_NAME : TestService DEPENDENCIES : MSSQL$SQLEXPRESS SERVICE_START_NAME : NT AUTHORITY\LocalService
Execute the following to cheque the failure configuration:
Code linguistic communication: plaintext ( plaintext )
sc qfailure TestService
This shows that it's using the failure configuration specified in the batch install script:
[SC] QueryServiceConfig2 SUCCESS SERVICE_NAME: TestService RESET_PERIOD (in seconds) : 86400 REBOOT_MESSAGE : COMMAND_LINE : FAILURE_ACTIONS : RESTART -- Delay = 60000 milliseconds. RESTART -- Delay = 60000 milliseconds.
Uninstall the service
The following batch file uninstalls the service and outputs the results to a timestamped log:
Code language: plaintext ( plaintext )
@ECHO OFF REM Get log file name with timestamp for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') practise (set ts=%%a) fix LogName="C:\logs\uninstallTestService%ts:~0,8%%ts:~viii,iv%%ts:~12,ii%.log" REM Install service set servicePath="C:\Services\TestService.exe" "C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% -u >> %LogName%
Running this uninstalls the service and outputs to a timestamped log file (ex: C:\logs\uninstallTestService20210205074023.log).
You tin can bank check if the service is uninstalled by checking if sc query returns an mistake, like this:
Code language: plaintext ( plaintext )
C:\WINDOWS\system32>sc query TestService [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not be every bit an installed service.
Source: https://makolyte.com/install-and-configure-a-windows-service-from-the-command-line/
Posted by: powerrince1956.blogspot.com

0 Response to "How To Install Windows Service Using Installutil Exe"
Post a Comment