Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELPneed script to stop multiple services on a remote machine

Status
Not open for further replies.

picnmix

Technical User
Apr 8, 2005
50
GB
Hi,
As the title says I need help stopping multiple services on a remote machine. I can find lots of examples of stopping a service on a remote machine but I want to stop about 6 services. I dont want to repeat the same bit of code over and over again for each service I want an array filled with the service names and then it loops round and stops each one. I don't want error checking or any echo stuff.
Oh sorry and can it be in vbscript/wmi

can anyone help I can't code so I am stuck!
Cheers picnmix
 
A starting point:
For Each strService In Array("service 1", "service 2")
' your stuff for strService here
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I have tried to mash your advance and the existing code together and I can't understand whats going on. I have NO knowledgeon this subject. it keeps saying I need a Next statement at the end of the script.Why?

Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService

strComputer = "acc3"



' +++++ MSExchangeMTA Service +++++

' NB strService is case sensitive.
For Each strService In Array("MSExchangeMTA", "MSExchangeMGMT")

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ="& strService & " ")

For Each objService in colListOfServices

objService.StartService()
WScript.Sleep 1500
Next
 
As you have 2 For you need 2 Next
So add a Next instruction at the end of the code.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I've put another next in but I get this message now
WINDOWS SCRIPT HOST
script: c:\test.vbs
line: 17
char: 1
error etc......
etc...

I dont know what is wrong any ideas?

Cheers
 
Sorry, a google search on "error etc......" gave no meaningful stuff.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
[tt]'NB strService is case sensitive.
[blue]Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")[/blue]
For Each strService In Array ("MSExchangeMTA", "MSExchangeMGMT")

[red]'[/red]Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name =[red]'[/red]"& strService & "[red]'[/red]")

For Each objService in colListOfServices

objService.StartService()
WScript.Sleep 1500
Next
[red]Next[/red]
[/tt]
 
Sorry, a google search on "error etc......" gave no meaningful stuff.

ROFL!!!!
_ROFL.gif


I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Wouldn't it be possible to reduce this to one for loop by using the array inside the query?
Something like:
Code:
Option Explicit


Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService
Dim servList

servList = Array("MSExchangeMTA", "MSExchangeMGMT")
strComputer = "acc3"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name IN ('"& Join(servList,"','") & "')")

For Each objService in colListOfServices
   objService.StartService()
   WScript.Sleep 1500
Next

I haven't done much with service queries, so I don't know if they support IN (or require single quotes around service names), but if it does, this makes more sense to me than a double for loop. If I had a windows machine powered on at the moment I would try, but I'm feeling lazy thi morning...

-T

 
tsujis first post meets the requirement to only query WMI once for a list of services....(the hit for returning all services rather than those contained in the array i dont too bad :))
 
Hmm, well even if IN isn't suppoerted, there is always the poormans IN:
Code:
Option Explicit


Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService
Dim servList

servList = Array("MSExchangeMTA", "MSExchangeMGMT")
strComputer = "acc3"

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '"& Join(servList,"' OR Name = '") & "'")

For Each objService in colListOfServices
	objService.StartService()
	WScript.Sleep 1500
Next

And as far as I can tell, without the sleep in either script, it runs twice as fast doing it in a single query.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top