Discussion:
VBScript that will stop then start two services and pop up a message box afterwards
(too old to reply)
Spin
2009-04-09 02:26:15 UTC
Permalink
Gurus,

I'm looking for a VBScript that will stop then start two services (call them
service1 and service2) and after they are started pop up a message box
stating services have been started.
--
Spin
Pegasus [MVP]
2009-04-09 05:53:08 UTC
Permalink
Post by Spin
Gurus,
I'm looking for a VBScript that will stop then start two services (call
them service1 and service2) and after they are started pop up a message
box stating services have been started.
--
Spin
Here is a simple way:
@echo off
net stop service1
net stop service2
net start service1
net start service2
echo msgbox "The services have been restarted" > "%temp%\temp.vbs"
wscript "%temp%\temp.vbs"
Richard Mueller [MVP]
2009-04-09 16:17:21 UTC
Permalink
Post by Spin
Gurus,
I'm looking for a VBScript that will stop then start two services (call
them service1 and service2) and after they are started pop up a message
box stating services have been started.
--
Spin
From the Microsoft TechNet Script Center, a VBScript solution using WMI to
stop a service (and any dependent services):

http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb25.mspx

Similarily, to start a service (and dependent services):

http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb23.mspx

If the computer is remote, substitute the NetBIOS name of the computer for
strComputer. Assuming no dependent services, the script for two services
could be similar to:
==========
' Bind to WMI namespace.
strComputer = "TestComputer"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")

' Retrieve services.
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='Service1' Or Name='Service2'")

' Stop each service.
For Each objService In colServiceList
lngError = objService.StopService()
Next

' Pause 20 seconds to allow time for the services to stop.
Wscript.Sleep 20000

' Start each service.
For Each objService In colServiceList
lngError = objService.StartService()
Next
===========
I specify authenticationLevel because some documentation indicates this may
be necessary on newer OS's. You can enumerate the list of services more than
once as long as you have not specified the WBEM_FLAG_FORWARD_ONLY flag in
the WQL (WMI Query Language) statement.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Spin
2009-04-14 17:48:56 UTC
Permalink
Nice. What does "colServiceList" stand for?
Post by Richard Mueller [MVP]
From the Microsoft TechNet Script Center, a VBScript solution using WMI to
http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb25.mspx
http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb23.mspx
If the computer is remote, substitute the NetBIOS name of the computer for
strComputer. Assuming no dependent services, the script for two services
==========
' Bind to WMI namespace.
strComputer = "TestComputer"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")
' Retrieve services.
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='Service1' Or
Name='Service2'")
' Stop each service.
For Each objService In colServiceList
lngError = objService.StopService()
Next
' Pause 20 seconds to allow time for the services to stop.
Wscript.Sleep 20000
' Start each service.
For Each objService In colServiceList
lngError = objService.StartService()
Next
===========
I specify authenticationLevel because some documentation indicates this
may be necessary on newer OS's. You can enumerate the list of services
more than once as long as you have not specified the
WBEM_FLAG_FORWARD_ONLY flag in the WQL (WMI Query Language) statement.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Richard Mueller [MVP]
2009-04-14 22:06:49 UTC
Permalink
It is a variable that refers to an collection of service objects. The WMI
query returns this collection. The reference is made using a Set statement
because it is a collection of objects. The name colServiceList was chosen in
one of the linked examples. "col" refers to collection. You can actually use
any valid name you desire.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Post by Spin
Nice. What does "colServiceList" stand for?
Post by Richard Mueller [MVP]
From the Microsoft TechNet Script Center, a VBScript solution using WMI
http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb25.mspx
http://www.microsoft.com/technet/scriptcenter/scripts/os/services/ossvvb23.mspx
If the computer is remote, substitute the NetBIOS name of the computer
for strComputer. Assuming no dependent services, the script for two
==========
' Bind to WMI namespace.
strComputer = "TestComputer"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" _
& strComputer & "\root\cimv2")
' Retrieve services.
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='Service1' Or
Name='Service2'")
' Stop each service.
For Each objService In colServiceList
lngError = objService.StopService()
Next
' Pause 20 seconds to allow time for the services to stop.
Wscript.Sleep 20000
' Start each service.
For Each objService In colServiceList
lngError = objService.StartService()
Next
===========
I specify authenticationLevel because some documentation indicates this
may be necessary on newer OS's. You can enumerate the list of services
more than once as long as you have not specified the
WBEM_FLAG_FORWARD_ONLY flag in the WQL (WMI Query Language) statement.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Spin
2009-04-23 23:15:43 UTC
Permalink
Post by Richard Mueller [MVP]
It is a variable that refers to an collection of service objects. The WMI
query returns this collection. The reference is made using a Set statement
because it is a collection of objects. The name colServiceList was chosen
in one of the linked examples. "col" refers to collection. You can
actually use any valid name you desire.
Richard, thanks. U R DA MAN!!!!!

Loading...