Greetings: I need to start by saying I have NO VB scripting experience. I have been creating scripts by copying others and pasting them together. This brings me to where I am now. I want to shut down a list of services. I figure I should create a list or array and use the service name as a variable within a sub routine. I can't figure this one out.
Something like this would be the subroutine but I don't know how to add the variable service names into this routine. I need to shut down 8 services on the client machines.
Option ExplicitDim strComputer, objWMIService, colServiceList, objService, errReturnCodestrComputer = "."Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = 'Alerter'")
For Each objService in colServiceList If objService.State = "Running" Then objService.StopService() Wscript.Sleep 5000 End IfNext
Control List of Services
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- mmacdonald
- Posts: 44
- Last visit: Thu Apr 07, 2016 7:53 am
Control List of Services
Joel -
That definately works. Thank you. There is something additional I need a pointer on. When the service name has a space in it, the script fails to shut it down. ie: the service name "DNS client" or "FTP Publishing" would not be shut down.
Any suggestions?
That definately works. Thank you. There is something additional I need a pointer on. When the service name has a space in it, the script fails to shut it down. ie: the service name "DNS client" or "FTP Publishing" would not be shut down.
Any suggestions?
- jdelatorre@hfinc.com
- Posts: 54
- Last visit: Wed Jan 30, 2008 1:42 am
Control List of Services
I believe your using the "Display name" for the service. You need to use the "Service Name".so for DNS client the Service name is "DNSCache"for Ftp Publishing I think the name is "MSFTPSVC"
- rodstewart
- Posts: 31
- Last visit: Mon Jan 16, 2012 6:28 am
Control List of Services
With a small modification, this script should work using the display name of the service, which may save you some time or may not:arrServices = Array("service1","service2","service3","etc")strComputer = "."Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")For Each Service In arrServices Set colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where DisplayName = '"&Service&"'") For Each objService in colServiceList If objService.State = "Running" Then WScript.Echo "running" Wscript.Sleep 5000 End If NextNext Cheers!
- mmacdonald
- Posts: 44
- Last visit: Thu Apr 07, 2016 7:53 am