?
Solved

Script to restart the service

Posted on 2009-05-13
7
Medium Priority
?
829 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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