23
June
2026
Script for restarting a service in MS Windows
16:48

Script for restarting a service in MS Windows

23 June 2026 16:48

A script for restarting a service (for adding to Task Scheduler) in Windows.

The "Hasp Loader" service, which loads keys for the network license of the 1C financial software, was periodically displaying a message about a missing network license. The solution, suggested by 1C support, was to restart the "Hasp Loader" service once a day using Task Scheduler. Quote: "Hasp Loader is running out of 'zero' licenses, which are used when logging in." The task is to write a script to stop and start the "Hasp Loader" service.

Below is the text of a VBS (VB Shell) script I developed to restart the service with the name specified in the sName variable. The script uses WMI (Windows Management Instrumentation) programming. The license is GPL.

On Error Resume Next
sName="Hasp Loader"
Set objService = GetObject _
("winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2")
If Err.Number  0 Then
    WScript.Echo Err.Number & ": " & Err.Description
    WScript.Quit
End If

If sName="" Then
    WScript.Echo "The service name is empty."
    WScript.Quit
End If

For Each objS In objService.ExecQuery _
("Select * FROM Win32_Service WHERE Name ='" & sName & "'" )
    Exit For
Next
If IsObject (objS) Then 
 WScript.Echo objS.Name & " - StopService()"
 ret = objS.StopService()
 DebugPrint ret

 WScript.Sleep 1500 ` 1,5s

 WScript.Echo objS.Name & " - StartService()"
 ret = objS.StartService()
 DebugPrint ret

Else
 WScript.Echo sName & " - service not found"
End If

Sub  DebugPrint(number)
Select Case number
   Case 0
      WScript.Echo "Done!"
   Case 1
      WScript.Echo "Error. Not supported"
   Case 2
      WScript.Echo "Error. No user rights"
   Case 3
      WScript.Echo "Error. Depends"
   Case 4
      WScript.Echo "Error. Wrong request"
   Case 5
      WScript.Echo "Error. Wrong state"
   Case 6
      WScript.Echo "Error. Not started"
   Case 7
      WScript.Echo "Error. Timed out"
   Case 8
      WScript.Echo "Error. Unknown failure"
   Case 9
      WScript.Echo "Error. Wrong path"
   Case 10
      WScript.Echo "Error. Already started"
   Case 11
      WScript.Echo "Error. DB is locked"
   Case 12
      WScript.Echo "Error. Dependency is deleted"
   Case 13
      WScript.Echo "Error. Dependency not found"
   Case 14
      WScript.Echo "Error. Disabled"
   Case 15
      WScript.Echo "Error. No authenticity"
   Case 16
      WScript.Echo "Error. Uninstalling"
   Case 17
      WScript.Echo "Error. No exec process"
   Case 18
      WScript.Echo "Error. Dependency is cycled"
   Case 19
      WScript.Echo "Error. Already started by this name"
   Case 20
      WScript.Echo "Error. Bad chars in name"
   Case 21
      WScript.Echo "Error. Bad parameters"
   Case 22
      WScript.Echo "Error. Bad account"
   Case 23
      WScript.Echo "Error. Service exists"
   Case 24
      WScript.Echo "Error. Service is paused"
End Select
End Sub

Running a script in the Windows command line:

cscript.exe hasp.vbs

Running a script in Task Scheduler

  • Simple task
  • Action: cscript,exe
  • Parameter: //B hasp.vbs

It is recommended to specify the full paths to the files, for example: C:\Windows\system32\cscript.exe and C:\Windows\hasp.vbs
and assign permissions only to the Administrator, who will restart the service using Task Scheduler.


Sources:


The list of return codes for the StopService() and StartService() methods is given below:

0 Request accepted.
1 Request not supported.
2 User did not have the required access.
3 The service cannot be stopped because other running services depend on it.
4 The requested control code is invalid or unacceptable for this service.
5 The requested control code cannot be sent to the service because the service state (Win32_BaseService. State Property ) is 0, 1, or 2.
6 The service is not running.
7 The service did not respond to the start request within the allotted time.
8 An unknown failure occurred while starting the service.
9 The directory path to the service executable was not found.
10 The service is already running.
11 The database for adding a new service is locked.
12 A dependency on which this service depends has been removed from the system.
13 This service could not find a service required by a dependent service.
14 This service has been disabled on the system.
15 This service does not support the authentication required to run on the system.
16 This service is being removed from the system.
17 The service does not have a thread of execution.
18 The service has circular dependencies when starting.
19 A service is running with the same name.
20 The service name contains invalid characters.
21 The service received invalid parameters.
22 The account under which this service is running is invalid or does not have permission to run the service.
23 The service exists in the database of services available on the system.
24 The service is currently paused on the system.



Related publications