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

VBS - Services Running? - Please help.

36 views
Skip to first unread message

John

unread,
Mar 28, 2003, 9:16:11 AM3/28/03
to
Firstly, thank you for your input. This group is da bomb. lol

Is there a way to determine via vbs, whether or not a service is
running on a specific machine in ou network? Then assign a value based
on whether or not its running.

Eg. If ftp service is running then V_Running = True....

Thank you.

JJ

Torgeir Bakken (MVP)

unread,
Mar 28, 2003, 5:54:36 PM3/28/03
to
John wrote:

Hi

ADSI's WinNT provider can be used...

Short version with no error/status checking:

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
bServiceRunning = True
Else
bServiceRunning = False
End If

Long version with error/status checking:


sNode = "some computer name or IP address here"

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(sCompName, sServiceName)

' init value for the boolean value
bServiceRunning = False

Const ADS_SERVICE_RUNNING = 4

' ping the computer to see if it is online
If IsConnectible(sCompName, "", "") 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

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

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\runresult.tmp"

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

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

sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile(sTempFile)

Select Case InStr(sResults,"TTL=")
Case 0 IsConnectible = False
Case Else IsConnectible = True
End Select
End Function

--
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/scriptcenter


John

unread,
Mar 29, 2003, 1:24:13 PM3/29/03
to
perfect. thank you.
0 new messages