Script to restart the service

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
Who is Participating?
Create restart_service.bat

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

NET START | FIND "Automatic Updates"
NET STOP "wuauserv"

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

Open in new window

Example modified from code found at

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

' ReStartService.vbs
' Sample script to Stop or Start a Service
' 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
WSCript.Sleep 1500
'WScript.Echo "Your "& strService & " service has Started" 
' End of Example WMI script to Start / Stop services

Open in new window

anuroopkoka2005Author Commented:
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.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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?
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

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.
anuroopkoka2005Author Commented:
Thanks for the script
Works great
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.