Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to restart the service

Posted on 2009-05-13
7
Medium Priority
?
835 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
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.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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 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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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