MichaelBalack
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=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("S elect * 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.
Const strSVCName = "DNS Client"
Dim objWMIService, colServices, objService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colServices = objWMIService.ExecQuery("S
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.
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
ASKER
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Lee W,
Yes, I tested and it works perfectly.
Thanks a lot
Yes, I tested and it works perfectly.
Thanks a lot
ASKER
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"?
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
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.
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
OrIf Left(objService.DisplayName, 16) = "windows defender" Then 'Found
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.
ASKER
Thanks for expert-Lee W in providing the suggestions. It works.
ASKER