Hi Friends,
I am trying to create a vbscript which use a input file containing list of hostname of servers and then it check for a particular defined service. Below code is working fine.
But now i want to add a new feature so that script will also tell the status of service. I also want to know that if the defined service is exsist then what is the status of service (Start- Stoped - or Paused). Please help me with my code to display the serive status also.
'--------------------8<----------------------
' Service name to look for
sServiceName = "Dfs"
' File containing server names
sInpFile = "serverlist.txt"
' Server/service status will be written here
sStatusFile = "DFS_ServiceQueryResults_Current.txt"
' FileSystemObject.CreateTextFile
Const OverwriteIfExist = -1
Const FailIfExist = 0
' FileSystemObject.OpenTextFile
Const OpenAsDefault = -2
Const CreateIfNotExist = -1
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Get server names into an array:
Set f = oFSO.OpenTextFile (sInpFile, _
ForReading, FailIfNotExist, OpenAsASCII)
Dim aServers()
i = 0
Do Until f.AtEndOfStream
sLine = Trim(f.ReadLine)
If Not sLine = "" Then
ReDim Preserve aServers(i)
aServers(i) = sLine
'aServers(i) = Left(sLine, InStr(sLine, ",") - 1)
i = i + 1
End If
Loop
f.Close
' Create status file
Set fStatusFile = oFSO.CreateTextFile(sStatusFile, _
OverwriteIfExist, OpenAsASCII)
' Loop through the server array
' (testing if the servers are connectible using ping)
For Each sServer In aServers
' test with ping
If IsConnectible(sServer,"","") Then
On Error Resume Next
Set oServer = GetObject("WinNT://" & sServer & ",computer")
If Err.Number <> 0 Then
sMsg = sServer & ", " & Now & ", " & "Could not connect with ADSI"
Else
Set oService = oServer.GetObject("Service", sServiceName)
If Err.Number <> 0 Then
sMsg = sServer & ", " & Now & ", " & "Service does not exist"
Else
sMsg = sServer & ", " & Now & ", " & "Service exists"
End If
End If
Else
sMsg = sServer & ", " & Now & ", " & "Could not connect with ping"
End If
fStatusFile.WriteLine sMsg
Next
fStatusFile.Close
Function IsConnectible(sHost, iPings, iTO)
' Returns True or False based on the output from ping.exe
' 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 = 5
If iTO = "" Then iTO = 1000
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)
IsConnectible = CBool(InStr(sResults, "TTL="))
End Function
'--------------------8<----------------------