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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stopping services via vb script 3

Status
Not open for further replies.

ArmyITGuy

MIS
Nov 3, 2003
56
US
Not sure if this is the right forum. I'm looking to stop a number of application specific services, that need to be stopped and started in a particular order on a somewhat frequent basis.

The goal here would be a script that "power users" can use to stop services for one application without a RDC session.

I tried batch files to no avial, so now I'm onto a vbs script. I think it's a verbiage/semantics thing.
 
Basic script from MS Script Repository.

Stopping a Service and it's dependencies:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
& "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
errReturn = objService.StopService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
errReturn = objService.StopService()
Next


Starting a service and it's dependecies:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
errReturn = objService.StartService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
& "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
objService.StartService()
Next
 
WhoKilledKenny, just to make sure that the correct services are stopped/started, wanted to highlight the area which ArmyITGuy should look at:


Stopping a Service and it's dependencies:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
& "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
errReturn = objService.StopService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
errReturn = objService.StopService()
Next

I also wondered why you didn't use the "code", but I kept getting a cold fusion error when I used it. Did you?
 
Yes, you would place the service name there. Also, if running the script remotely you would want to change strComputer="." (which means local machine) to strComputer="servername" - keep the quotes in tack as this is a string.
 
If you are getting erros from the code above, note that the underscore means it should be one line.
Try changing Set ColServiceLise........Query _ to one continuous line. So ...ExecQuery("SELECT * FROM...
 
Cool...
Stopping service & Dependents.
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery("Associators of " _
    & "{Win32_Service.Name='NetDDE'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

For Each objService in colServiceList
    objService.StopService()
Next

Wscript.Sleep 20000

Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='NetDDE'")
For Each objService in colServiceList
    errReturn = objService.StopService()
Next

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

Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='NetDDE'")

For Each objService in colServiceList
    errReturn = objService.StartService()
Next

Wscript.Sleep 20000

Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='NetDDE'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Dependent" )
For Each objService in colServiceList
    objService.StartService()
Next
 
To continue on WhoKilledKenny's post....

where NetDDE is the service you want to stop/start.....
 
OK I assume I would use the "service Name", or would I use the "display name"? Or does it matter?
 
At a command promt - type in "net start" (without the quotes) that will give you the name you can use for the service you wish to run the script against.
 
I should clarify. There is an application that utilizes about 2 dozen services, sometimes the application hangs and the mfg recommends stopping the services in a particular order, then restarting the services.

They have a batch file that works great if you are logged onto the server via remote desktop.

I'm trying to use a vbs script so a user can accomplish the same thing without being logged into the server's RDC.
 
Depending on how versed you are with VBScript. What I would do is create subroutines for stopping and starting each service. The subroutine would look like the script above each sub would have the name of the service. Then in the body of my code I would just call the subroutines in order to which the services have to be stopped and started. You could use the sleep command between calls or write some type of condition statement that will run to the next sub when the first process is over. If you are new to VBScript and don't have the time to dig into it (but when you do, do it...) Create a script for each service using the code above and manually launch each one in the order the mfg recommends...
 
The second set is what you really want to use. The first script may stop all the services for that particular application, where as the second script you can break down the services. As WhoKilledKenny points out, the power of vbscript is awesome, and take the time to learn it. Once it's learned, it can lead to BT (Beach Time) as a colleague once referred it as.
 
Awesome. I'll try a couple of services stop & start and then see if I can roll it into a more inclusive script.

...no I don't have a lot of vbs experience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top