Solved

Script to restart the service

Posted on 2009-05-13
7
799 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

18 Experts available now in Live!

Get 1:1 Help Now