Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > VBScript that will stop then start two services and pop up a message box afterwards

Reply
Thread Tools Display Modes

VBScript that will stop then start two services and pop up a message box afterwards

 
 
Spin
Guest
Posts: n/a

 
      04-09-2009
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

 
Reply With Quote
 
 
 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      04-09-2009

"Spin" <> wrote in message
news:...
> 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"


 
Reply With Quote
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      04-09-2009

"Spin" <> wrote in message
news:...
> 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/scr.../ossvvb25.mspx

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

http://www.microsoft.com/technet/scr.../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,authenticationLev el=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
--


 
Reply With Quote
 
Spin
Guest
Posts: n/a

 
      04-14-2009
Nice. What does "colServiceList" stand for?

"Richard Mueller [MVP]" <rlmueller-> wrote in
message news:...
> From the Microsoft TechNet Script Center, a VBScript solution using WMI to
> stop a service (and any dependent services):
>
> http://www.microsoft.com/technet/scr.../ossvvb25.mspx
>
> Similarily, to start a service (and dependent services):
>
> http://www.microsoft.com/technet/scr.../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,authenticationLev el=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
> --


 
Reply With Quote
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      04-14-2009
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
--

"Spin" <> wrote in message
news:...
> Nice. What does "colServiceList" stand for?
>
> "Richard Mueller [MVP]" <rlmueller-> wrote in
> message news:...
>> From the Microsoft TechNet Script Center, a VBScript solution using WMI
>> to stop a service (and any dependent services):
>>
>> http://www.microsoft.com/technet/scr.../ossvvb25.mspx
>>
>> Similarily, to start a service (and dependent services):
>>
>> http://www.microsoft.com/technet/scr.../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,authenticationLev el=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
>> --

>



 
Reply With Quote
 
Spin
Guest
Posts: n/a

 
      04-23-2009
"Richard Mueller [MVP]" <rlmueller-> wrote in
message news:...
> 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!!!!!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop/Start Services Patrick Goovaerts Windows Vista General Discussion 2 11-24-2008 09:50 PM
Can't start or stop services... Lars Windows Server 1 12-14-2005 12:50 PM
Re: Help Please! W2K Server - Services Won't Start or Stop Dave Patrick Windows Server 1 10-17-2005 11:54 PM
CANNOT OPEN CONTROL PANEL, START/STOP SERVICES, START DISK MANAGER anyichux Windows Update 4 08-29-2005 08:51 PM
Stop/Start services James S Windows Server 1 06-25-2004 09:41 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59