Link to home
Start Free TrialLog in
Avatar of MichaelBalack
MichaelBalackFlag for Singapore

asked on

How to display the status of the selective windows services using vbs?

This is intend to use a vbs script to show the status/startup of selective list of services for Windows 7/10 machine. This script is going to be executed locally on the selected machine, and output the results to a file. Please see the vbs contents,

  Const strSVCName = "DNS Client"
  Dim objWMIService, colServices, objService, strComputer
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  Set colServices = objWMIService.ExecQuery("Select * from Win32_Service WHERE DisplayName = '" & strSVCName & "'")

  For Each objService in colServices
   Wscript.Echo strSVCName _
   & vbcrlf & "Status: " & objService.State _
   & vbCrLf & "Startup Type: " & objService.StartMode
 Next

So far, I can only get a single service to work. However, I wanted to get an "array" of services, for example, Windows defender firewall, symantec endpoint protection, and so on. How to do it?

thanks in advance.
Avatar of MichaelBalack
MichaelBalack
Flag of Singapore image

ASKER

Services show in display name
Avatar of Lee W, MVP
Don't specify WHERE.  WHERE is criteria - you don't want criteria - you want everything.
Here's my version of your script that accomplishes what you want:

Option Explicit

'Const strSVCName = "DNS Client"
Dim objWMIService, colServices, objService, strComputer

strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Set colServices = objWMIService.ExecQuery("Select * from Win32_Service WHERE DisplayName = '" & strSVCName & "'")
Set colServices = objWMIService.ExecQuery("Select * from Win32_Service")

For Each objService in colServices
	Wscript.Echo "Display Name: " & objService.DisplayName & vbcrlf & "Status: " & objService.State & vbCrLf & "Startup Type: " & objService.StartMode & vbcrlf & "-------------------------------"
Next

Open in new window

Hi Lee W,

Thanks for the prompt reply.

How about if just want to see the status/auto start for the these few services only,

                 windows defender firewall
                 windows defender security center service
                 symantec endpoint protection
                 symantec managemnet agent

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Lee W,

Yes, I tested and it works perfectly.

Thanks a lot
Hi Lee W,

Instead of writing
   strCheckForServices = "windows defender firewall,windows defender security center service,symantec endpoint protection,symantec    management agent"

can I use wildcard? for example, write "windows defender *" for all services start with "windows defender"?
No, vb doesn't understand wild cards.

Using a string and splitting it into an array allows the greatest flexibility.  If you wanted you could do four separate checks instead of using the array (or perhaps two) and use
If instr(objService.DisplayName, "windows defender") > 0 Then 'found

Open in new window

Or
If Left(objService.DisplayName, 16) = "windows defender" Then 'Found

Open in new window


But in both examples you then have to have two if statements... and as soon as you want to add more services, you're adding more if statements.

Using the above script, you can add as many services as you like to the text string and separate them by commas.
Thanks for expert-Lee W in providing the suggestions. It works.