Stopping a service and waiting in VBScript

How to stop a service and wait for its completion in vbscript?

I still have this:

For Each objService in colServiceList If objService.DisplayName = "<my display name>" Then objService.StopService() End If Next 

Googling made a suggestion to use objService.WaitForStatus( ServiceControllerStatus.Stopped ) , but it works, which gives me the error "Required object:" ServiceControllerStatus ".

+4
source share
1 answer

The WaitForStatus method is not included in the Win32_Service WMI interface. I think from the .NET class. There is no equivalent WMI method.

To get the updated status, you must query the WMI service object. You can then exit the loop as soon as the status changes to β€œStopped”.

 Option Explicit Const MAX_ITER = 30, _ VERBOSE = True Dim wmi, is_running, iter Set wmi = GetObject("winmgmts:") For iter = 0 To MAX_ITER StopService "MSSQL$SQLEXPRESS", is_running If Not is_running Then Log "Success" WScript.Quit 0 End If WScript.Sleep 500 Next Log "max iterations exceeded; timeout" WScript.Quit 1 ' stop service by name. returns false in is_running if the service is not ' currently running or was not found. Sub StopService(svc_name, ByRef is_running) Dim qsvc, svc is_running = False Set qsvc = wmi.ExecQuery( _ "SELECT * FROM Win32_Service " & _ "WHERE Name = '" & svc_name & "'") For Each svc In qsvc If svc.Started Then is_running = True If svc.State = "Running" Then svc.StopService Log svc.Name & ": " & svc.Status End If Next End Sub Sub Log(txt) If VERBOSE Then WScript.StdErr.WriteLine txt End Sub 
+4
source

Source: https://habr.com/ru/post/1310547/


All Articles