Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Monitoring Services and taking action

3 views
Skip to first unread message

Star4

unread,
Mar 10, 2004, 12:20:59 PM3/10/04
to
Hi,

Newbie - be kind.

I am trying create a script that will stop a service,
sleep for X seconds, start the service again and if the
service has not started within Y amount of time write an
event to the application log.

The stopping and starting of the service as well as the
sleeping bit is easy, however, I can't seem to get the
status checking of the service right.

How can I check if the service is running and if not,
kick off the eventlog writing bit.

You kind assistance will be appreciated.

Thanks,

Star4
South Africa

Torgeir Bakken (MVP)

unread,
Mar 10, 2004, 1:29:05 PM3/10/04
to
Star4 wrote:

> I am trying create a script that will stop a service,
> sleep for X seconds, start the service again and if the
> service has not started within Y amount of time write an
> event to the application log.
>
> The stopping and starting of the service as well as the
> sleeping bit is easy, however, I can't seem to get the
> status checking of the service right.
>
> How can I check if the service is running and if not,
> kick off the eventlog writing bit.

Hi

WScript.Shell's LogEvent method can write to the Application log.

WSH 5.6 documentation (local help file) can be downloaded from here if you
haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp

ADSI's WinNT provider can be used to check the service status, short
version with no error/status checking:

'--------------------8<----------------------
Const ADS_SERVICE_RUNNING = 4
sNode = "some computer name or IP address here"
sServiceName = "fax"

Set oComputer = GetObject("WinNT://" & sNode & ",computer")
Set oService = oComputer.GetObject("Service", sServiceName)

If oService.Status = ADS_SERVICE_RUNNING Then
WScript.Echo sServiceName & " service is running"
Else
WScript.Echo sServiceName & " service status is stopped or unknown"
End If
'--------------------8<----------------------

Long version with error/status checking (e.g. it pings a remote
computer to see if it is available):

'--------------------8<----------------------
' get local computer name
Set oWshNet = CreateObject("Wscript.Network")
sNode = oWshNet.ComputerName

sServiceName = "fax"

If IsServiceRunning(sNode, sServiceName) Then
WScript.Echo sServiceName & " service is running"
Else
WScript.Echo sServiceName & " service status is stopped or unknown"
End If


Function IsServiceRunning(ByVal sCompName, ByVal sServiceName)

' to run the check against the local computer, sCompName can
' be ".", "" as well as the actual computer name.

' init value for the boolean values
bServiceRunning = False
bNodeAvailable = False

Const ADS_SERVICE_RUNNING = 4

Set oWshNet = CreateObject("Wscript.Network")
sLocCompName = LCase(oWshNet.ComputerName)

If LCase(sCompName) = sLocCompName Or sCompName = "" Or sCompName = "." Then
sCompName = sLocCompName
bNodeAvailable = True
' ping the computer to see if it is online
ElseIf IsConnectible(sCompName, "", "") Then
bNodeAvailable = True
End If

If bNodeAvailable Then
On Error Resume Next

Set oComputer = GetObject("WinNT://" & sCompName & ",computer")
If Err.Number <> 0 Then
'WScript.Echo sCompName & " is online but not available"
Else
Err.Clear
Set oService = oComputer.GetObject("Service", sServiceName)
If Err.Number <> 0 Then
'WScript.Echo sCompName & " is likely missing service " & sServiceName
Else
Err.Clear
If oService.Status = ADS_SERVICE_RUNNING Then
bServiceRunning = True
'WScript.Echo sServiceName & " is running on " & sCompName
Else
'WScript.Echo sServiceName & " is not running on " & sCompName
End If
End If
End If
Else
'WScript.Echo sCompName & " is not online"
End If

IsServiceRunning = bServiceRunning

End Function

Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
'
' Author: Alex Angelopoulos/Torgeir Bakken
' Works an "all" WSH versions
' sHost is a hostname or IP
' iPings is number of ping attempts
' iTO is timeout in milliseconds
' if values are set to "", then defaults below used

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Dim oShell, oFSO, sTempFile, fFile

If iPings = "" Then iPings = 2
If iTO = "" Then iTO = 750

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

sTempFile = oFSO.GetSpecialFolder(2).ShortPath & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO _
& " " & sHost & ">" & sTempFile, 0 , True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, _
FailIfNotExist, OpenAsASCII)

Select Case InStr(fFile.ReadAll, "TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select

fFile.Close
oFSO.DeleteFile(sTempFile)

End Function
'--------------------8<----------------------

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide:
http://www.microsoft.com/technet/community/scriptcenter/default.mspx


anon...@discussions.microsoft.com

unread,
Mar 11, 2004, 12:13:36 AM3/11/04
to
Hi Torgeir,

Thanks for your response. I have been looking at using
WMI (see script below).

I have found that WScript.Shell's LogEvent method is
rather limited in view of the amount of info that you can
write to the Application log, which is why I prefer using
logevent.exe from the W2K Resource Kit.

In any event, from the script below you can see where I
am battling, i.e. the last section. Should you have any
comments, I would greatly appreciate it. I will however
look at the ADSI approach as well.

------
strComputer = "."
Set wshShell = WScript.CreateObject("WScript.Shell")
Dim strFail : strFail = "logevent -s E -r Messenger -e
100 -t 30000 Messenger has not restarted successfully." &
_
"Please inform Standby
Administrator."
Dim strSuccess : strSuccess = "logevent -s I -r
Messenger -e 100 -t 30000 'Messenger successfully
started.'"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer
& "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'Messenger'")
For Each objService in colListOfServices
objService.StopService()
WScript.Sleep 5000
WScript.Echo "Phase1"
objService.StartService()
WScript.Sleep 20000
WScript.Echo "Phase2"
' If objService.State = "Stopped" Then
' strFail
' Else If objService.State = "Running" Then
' strSuccess

Next
------

Many thanks,

Star4
South Africa

0 new messages