'Service restart script
'Written by L Mason
'Script checks to see if a service is running locally. If it isn't it restarts
it.
Option Explicit
Const ADS_SERVICE_RUNNING = &H0004
Const ADS_SERVICE_ERROR = &H0008
Dim LocalComputerName
Dim LocalService
Dim DisplayNotices
Dim LogFileName
Dim TimeOut
Dim Retries
Dim FSys
Dim objComputer
Dim objService
Dim tFile
Dim wshShell
LocalService = "Themes"
Retries = 10
'Create Object
Set wshShell = CreateObject("WSCript.Shell")
LocalComputerName = wshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName")
Set objComputer = Nothing
'enter your domain name here
Set objComputer = GetObject("WinNT://yourdomain/" & LocalComputerName &
",computer")
Set objService = Nothing
Set objService = objComputer.GetObject("service", LocalService)
If Err.Number <> 0 Then
'Error returned - could not connect to service
MsgBox "The '" & LocalService & "' service was not found on your computer. The
operation will now exit.", vbOkOnly+vbCritical
Err.Clear
ClearEnvironment -1
Else
'Query Status / Start
Timeout = 0
While objService.Status <> ADS_SERVICE_RUNNING
objService.Start
Sleep 1
TimeOut = TimeOut + 1
If TimeOut => Retries OR objService.Status = ADS_SERVICE_ERROR Then
If DisplayNotices = True Then
MsgBox "Unable to start the '" & LocalService & "' service on your computer. The
operation will now exit.", vbOkOnly+vbCritical, "Abort"
End If
ClearEnvironment -1
End If
Wend
End If
ClearEnvironment 0
'-------------------------------------
Sub ClearEnvironment(ExitCode)
On Error Resume Next
Set FSys = Nothing
Set tFile = Nothing
Set objComputer = Nothing
Set objService = Nothing
WScript.Exit ExitCode
End Sub
Sub Sleep(Seconds)
Dim Timer
Timer = Second(Time)+5
If Timer > 59 Then Timer = Timer - 59
While Timer > Second(Now)
Wend
End Sub
Function ResolveStatus(Status)
Const ADS_SERVICE_STOPPED = &H0001
Const ADS_SERVICE_START_PENDING = &H0002
Const ADS_SERVICE_STOP_PENDING = &H0003
Const ADS_SERVICE_RUNNING = &H0004
Const ADS_SERVICE_CONTINUE_PENDING = &H0005
Const ADS_SERVICE_PAUSE_PENDING = &H0006
Const ADS_SERVICE_PAUSED = &H0007
Const ADS_SERVICE_ERROR = &H0008
Dim Color
Dim StatusText
Select Case Status
Case ADS_SERVICE_STOPPED
StatusText = "Stopped"
Case ADS_SERVICE_START_PENDING
StatusText = "Start Pending"
Case ADS_SERVICE_STOP_PENDING
StatusText = "Stop Pending"
Case ADS_SERVICE_RUNNING
StatusText = "Running"
Case ADS_SERVICE_CONTINUE_PENDING
StatusText = "Continue Pending"
Case ADS_SERVICE_PAUSE_PENDING
StatusText = "Pause Pending"
Case ADS_SERVICE_PAUSED
StatusText = "Paused"
Case ADS_SERVICE_ERROR
StatusText = "ERROR"
End Select
ResolveStatus = StatusText
end Function
Monday, January 23, 2006
Service restart script
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment