Using VBScript, how can I check if the Spooler service is running and if it is not running?

I want to use VBScript to check if the Spooler service is running and if it is not running, the code below checks the status of the service, but I need help to change this so that I can check if it is running.

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colRunningServices 
    Wscript.Echo objService.DisplayName  & VbTab & objService.State
Next

Thanks a lot Steven

+3
source share
3 answers

How about something like that. This command will launch it if it is not already running. No need to check in advance.

Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "NET START spooler", 1, false
+5
source
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
    ("select State from Win32_Service where Name = 'Spooler'")

For Each objService in colRunningServices
    If objService.State <> "Running" Then
        errReturn = objService.StartService()
    End If
Next

Please note that you can also use objService.startedto check if it is running.

+1
source

, , Shell.Application:

Const strServiceName = "Spooler"

Set oShell = CreateObject("Shell.Application")
If Not oShell.IsServiceRunning(strServiceName) Then
  oShell.ServiceStart strServiceName, False
End If

:

Set oShell = CreateObject("Shell.Application")
oShell.ServiceStart "Spooler", False    ''# This returns False if the service is already running
0

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


All Articles