How To Install Windows Service Using Installutil Exe
In this article I'll show how to install and configure a Windows Service, query its condition and backdrop, and so how to uninstall it. I'll exist using the command line utilities installutil.exe and sc.exe to install and configure the service.
Add a service installer class to your Windows Service project
In club to utilize installutil.exe to install your Windows Service, you lot demand to add together a service installer class. Otherwise when yous attempt to install, you'll become the following error:
No public installers with the RunInstallerAttribute.Yes attribute could exist institute in the C:\Services\TestService.exe associates.
To create a service installer form:
- Open your service class file in blueprint mode in Visual Studio.
- Right-click > click Add Installer.
Subsequently you click Add Installer, it will create the service installer class and open up it in blueprint mode.
At a bare minimum, you demand to prepare the ServiceName and Business relationship properties.
- Click serviceInstaller1 to bring up its properties.
- Specify the ServiceName belongings.
Next, set the Account property. This property is the account that your service runs as, so choose whatsoever makes sense in your case (if you're non sure, enquire a security person).
- Click serviceProcessInstaller1 to bring up its properties.
- Gear up the Business relationship to whatever value is advisable in your state of affairs.
Install the service and configure it
The following batch file installs TestService.exe by using installutil.exe, configures the service with sc.exe, and starts the service using internet showtime:
Code language: plaintext ( plaintext )
@Repeat OFF REM Get log file proper name with timestamp for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-ix]') do (set ts=%%a) prepare 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 beginning automatically sc config TestService beginning= auto >> %LogName% REM On crash, restart after 1 infinitesimal sc failure TestService actions= restart/60000/restart/60000// reset= 86400 >> %LogName% REM Start the service net start TestService >> %LogName%
Running this installs the service and outputs to a timestamped log file (ex: C:\logs\installTestService20210205074016.log).
Note: Execute this by using Run as Administrator.
It configures the service with the post-obit backdrop:
- Depends on service MSSQL$SQLEXPRESS (SQL Server Express). This ways information technology won't run unless SQL Server Express is running. When SQL Server Limited restarts, information technology as well restarts TestService.
- Starts automatically.
- Restarts after 1 minute if the service crashes. Note: This is but configuring it to auto-restart twice. After two failures, information technology won't endeavour to auto-restart. It resets the failure counter afterwards 1 day.
Query the service properties and status
You can look at the service backdrop and condition either by looking in Services, or by querying the properties from the command line past using sc.exe.
Execute the post-obit to become the service properties and status:
Code language: plaintext ( plaintext )
sc qc TestService
This shows that the service is running and information technology's using the configuration specified in the batch install script (from the previous section):
Lawmaking language: plaintext ( plaintext )
[SC] QueryServiceConfig SUCCESS SERVICE_NAME: TestService TYPE : 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 Dominance\LocalService
Execute the following to cheque the failure configuration:
Code linguistic communication: plaintext ( plaintext )
sc qfailure TestService
This shows that information technology'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:
Lawmaking language: plaintext ( plaintext )
@ECHO OFF REM Get log file name with timestamp for /f %%a in ('wmic os go LocalDateTime ^| findstr ^[0-nine]') do (set ts=%%a) gear up LogName="C:\logs\uninstallTestService%ts:~0,8%%ts:~8,4%%ts:~12,2%.log" REM Install service fix 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).
Y'all tin can check if the service is uninstalled by checking if sc query returns an error, like this:
Code language: plaintext ( plaintext )
C:\WINDOWS\system32>sc query TestService [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does non exist as an installed service.
How To Install Windows Service Using Installutil Exe,
Source: https://makolyte.com/install-and-configure-a-windows-service-from-the-command-line/
Posted by: stewartarow1957.blogspot.com
0 Response to "How To Install Windows Service Using Installutil Exe"
Post a Comment