Solved

Script to restart the service

Posted on 2009-05-13
7
803 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now