Try this:
'===============================================================================
'= =
'= Created By: Devin H. Date: 11/28/2005 =
'= =
'= Name: ServiceStatus.vbs Version: 1.0 =
'= =
'= Notes: This script will check the status of the services listed =
'= If the service isn't running it will attempt to start it... =
'= =
'= Functions: =
'= CreateFile - Creates the log file =
'= Servicecheck - Checks the status on the service =
'= StartService - Starts the service =
'= WriteFile - Deletes/Creates the log file =
'= =
'===============================================================================
'===============================================================================
'= Get the path the script is running in... We save the logfile in the same dir
'===============================================================================
Set oShell = createObject("WScript.Shell")
Path = oShell.CurrentDirectory
File = "\logfile.log"
'===============================================================================
'= How many times do you want to try to start the service?
'===============================================================================
Keeptrying = 4
'===============================================================================
'= Create the logfile if it doesn't exist...
'===============================================================================
call CreateFile(Path & File)
'===============================================================================
'= List the monitored services here...
'===============================================================================
call servicecheck("Messenger")
'===============================================================================
'= Function Name: ServiceCheck Date: 11/28/2005 Version:1.0
'=
'= Variables: servicename
'=
'= Tell me the status on the service I passed you...
'=
'===============================================================================
function servicecheck(servicename)
'On Error Resume Next
strComputer = "."
set objWMIService = getObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service WHERE NAME ='" & servicename & "'")
for each objService in colRunningServices
If Not objService.State = "Running" Then
call WriteFile(Path & File, Time & " " & Date & " " & servicename & " - " & objService.State)
call startservice(servicename)
Else
call WriteFile(Path & File, Time & " " & Date & " " & servicename & " - " & objService.State)
End If
next
end function
'===============================================================================
'= Function Name: StartService Date: 11/28/2005 Version:1.0
'=
'= Variables: servicename
'=
'= Attempts to start the service it is passed... Uses a global variable...
'=
'===============================================================================
function startservice(servicename)
call WriteFile(Path & File, Time & " " & Date & " " & servicename & " - " & "Attempting to start...")
strComputer = "."
' NB strService is case sensitive.
set objWMIService = getObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='" & servicename & "'")
for each objService in colListOfServices
'objService.StopService()
objService.StartService()
WSCript.Sleep 15000
If objService.State = "Running" Then
call WriteFile(Path & File, Time & " " & Date & " " & servicename & " - " & "Successfully Started!")
Else
call WriteFile(Path & File, Time & " " & Date & " " & servicename & " - " & "Failed to start...")
if KeepTrying > 0 Then
KeepTrying = KeepTrying - 1
call startservice(servicename)
end if
end if
next
end function
'===============================================================================
'= Function Name: CreateFile Date: 10/19/2005 Version:1.0
'=
'= Variables: file
'=
'= If the log file doesn't exist it creates the file. You can't write to a
'= file that doesn't exist.
'=
'===============================================================================
function CreateFile(file)
set objFSO = createObject("Scripting.FileSystemObject")
if objFSO.FileExists(file) then
exit function
end if
set objFile = objFSO.CreateTextFile(file)
end function
'===============================================================================
'= Function Name: WriteFile Date: 11/28/2005 Version:1.0
'=
'= Variables: file, text
'=
'= This does the actually writing to the log file.
'=
'===============================================================================
function WriteFile(file, text)
const ForAppending = 8
set objFSO = createObject("Scripting.FileSystemObject")
set objTextFile = objFSO.OpenTextFile _
(file, ForAppending, true)
objTextFile.WriteLine(text)
objTextFile.Close
end function
Wscript.Echo "Done"