How Do I Install A Windows Service From The Command Line
In this article I'll show how to install and configure a Windows Service, query its status and properties, then how to uninstall it. I'll be using the control line utilities installutil.exe and sc.exe to install and configure the service.
Add a service installer form to your Windows Service project
In order to use installutil.exe to install your Windows Service, y'all demand to add a service installer class. Otherwise when you try to install, you'll go the post-obit error:
No public installers with the RunInstallerAttribute.Aye aspect could exist establish in the C:\Services\TestService.exe associates.
To create a service installer class:
- Open your service class file in design mode in Visual Studio.
- Right-click > click Add together Installer.
After you click Add Installer, information technology will create the service installer class and open up information technology in design fashion.
At a bare minimum, you demand to set the ServiceName and Account properties.
- Click serviceInstaller1 to bring upward its backdrop.
- Specify the ServiceName belongings.
Next, set the Account property. This property is the account that your service runs as, so choose whatever makes sense in your case (if you're not sure, ask a security person).
- Click serviceProcessInstaller1 to bring upwards its properties.
- Set the Account to whatever value is appropriate in your state of affairs.
Install the service and configure it
The following batch file installs TestService.exe past using installutil.exe, configures the service with sc.exe, and starts the service using internet start:
Code linguistic communication: plaintext ( plaintext )
@Echo OFF REM Get log file name with timestamp for /f %%a in ('wmic os go LocalDateTime ^| findstr ^[0-9]') do (prepare ts=%%a) set LogName="C:\logs\installTestService%ts:~0,eight%%ts:~8,four%%ts:~12,ii%.log" REM Install service set up servicePath="C:\Services\TestService.exe" "C:\Windows\Microsoft.Internet\Framework\v4.0.30319\installutil.exe" %servicePath% >> %LogName% REM Add dependency on SQL Server sc config TestService depend=MSSQL$SQLEXPRESS >> %LogName% REM Make start automatically sc config TestService start= motorcar >> %LogName% REM On crash, restart after i minute sc failure TestService deportment= restart/60000/restart/60000// reset= 86400 >> %LogName% REM Get-go 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 by using Run as Administrator.
It configures the service with the following backdrop:
- Depends on service MSSQL$SQLEXPRESS (SQL Server Express). This means it won't run unless SQL Server Express is running. When SQL Server Limited restarts, it as well restarts TestService.
- Starts automatically.
- Restarts after 1 minute if the service crashes. Annotation: This is just configuring it to auto-restart twice. After two failures, it won't try to auto-restart. It resets the failure counter later 1 day.
Query the service properties and condition
You can look at the service backdrop and status either by looking in Services, or past querying the properties from the command line by using sc.exe.
Execute the following to go the service properties and status:
Code language: plaintext ( plaintext )
sc qc TestService
This shows that the service is running and it'due south using the configuration specified in the batch install script (from the previous section):
Code linguistic communication: plaintext ( plaintext )
[SC] QueryServiceConfig SUCCESS SERVICE_NAME: TestService Blazon : 10 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 Say-so\LocalService
Execute the following to check the failure configuration:
Code language: plaintext ( plaintext )
sc qfailure TestService
This shows that information technology'due south 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 -- Filibuster = 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 linguistic communication: plaintext ( plaintext )
@ECHO OFF REM Become log file proper noun with timestamp for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-nine]') practice (set ts=%%a) gear up LogName="C:\logs\uninstallTestService%ts:~0,8%%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% -u >> %LogName%
Running this uninstalls the service and outputs to a timestamped log file (ex: C:\logs\uninstallTestService20210205074023.log).
You 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 not be as an installed service.
How Do I Install A Windows Service From The Command Line,
Source: https://makolyte.com/install-and-configure-a-windows-service-from-the-command-line/
Posted by: dodsonrangho.blogspot.com

0 Response to "How Do I Install A Windows Service From The Command Line"
Post a Comment