Solved

Script to restart the service

Posted on 2009-05-13
7
816 Views
Last Modified: 2012-05-06
Hi Experts,

I have restart the service every morning. So for that i need a Vbscript to do the same.
the Script should check if the service is completelly stopped and then start the service back.

Is it possible
0
Comment
Question by:anuroopkoka2005
7 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24373442
Example modified from code found at http://www.computerperformance.co.uk/vbscript/wmi_services.htm

Change "Alerter" below to your service name...

' ReStartService.vbs
' Sample script to Stop or Start a Service
' www.computerperformance.co.uk/
' Created by Guy Thomas December 2005 Version 2.4
' -------------------------------------------------------' 
Option Explicit
Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, intSleep 
strComputer = "."
 
'On Error Resume Next
' NB strService is case sensitive.
strService = " 'Alerter' "
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.StopService()
WSCript.Sleep 1500
objService.StartService()
Next 
'WScript.Echo "Your "& strService & " service has Started" 
WScript.Quit
' End of Example WMI script to Start / Stop services

Open in new window

0
 
LVL 4

Author Comment

by:anuroopkoka2005
ID: 24373556
Hello Sirbounty,

The script is not working. I changed the Alerters to Automatic Updates and no go.
It should check whether the service entered the stopped state. and then restart it.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24373630
Automatic Updates is not the service name...Auto Updates is launched under a svchost service, so that one is a bit trickier (you don't want to recycle svchost).
If you've got a problem with that service, I think you're better off fixing the problem, no?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 67

Expert Comment

by:sirbounty
ID: 24373643
You can accomplish a recycle of that service using a simple batch file...but again, I'd look into the underlying problem and try to resolve it...

And this can be converted to a vbscript, but something this simple would take a couple more lines of code, which is why I posted the bat solution:
net stop "automatic updates"
net start "automatic updates"

Open in new window

0
 
LVL 6

Expert Comment

by:MikeGGG
ID: 24373736
I am not a scripting guy but even I see that this script will not check if the service is stopped before trying to start it.
And, I think, in first line you should check the current status of the service.
0
 
LVL 1

Accepted Solution

by:
de3mka earned 500 total points
ID: 24373973
Create restart_service.bat

REM this will set the service start type to automatic
sc config "wuauserv" start= auto

NET START | FIND "Automatic Updates"
I
F %ERRORLEVEL%==1 GOTO START_SERVICE
NET STOP "wuauserv"

:START_SERVICE
NET START "wuauserv"


If you need VBScript then just run this batch file from WshShell (for example)
Option Explicit
 
Dim WshShell
Dim SourceDir
Dim retcode
 
Set WshShell = CreateObject("WScript.Shell")
SourceDir = Replace(WScript.ScriptFullName,WScript.ScriptName,"",1,1,1)
WshShell.Run(SourceDir & "restart_service.bat")
Set WshShell = Nothing
 
WScript.Quit(retcode)

Open in new window

0
 
LVL 4

Author Closing Comment

by:anuroopkoka2005
ID: 31580902
Thanks for the script
Works great
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question