0

I'm using this VBscript code to ping a list of computers in a notepad and store the ping results in a csv file.

All computers stored in a notepad file are now availables (cmd >>> ping servername), but the results in csv file are all DOWN.

I have tried also stored in notepad file one computer unavailable, but the results ping in a csv file are all DOWN...

Any suggestion?

My code below

dim strInputPath, strOutputPath, strStatus
dim objFSO, objTextIn, objTextOut

strInputPath = "C:\serverlist.txt"
strOutputPath = "C:\output.csv"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objTextIn = objFSO.OpenTextFile( strInputPath,1 )
set objTextOut = objFSO.CreateTextFile( strOutputPath )
objTextOut.WriteLine("computer,status")

Do until objTextIn.AtEndOfStream = True
    strComputer = objTextIn.ReadLine
        if fPingTest( strComputer ) then
             strStatus = "UP"
        else
             strStatus = "DOWN"
        end if
        objTextOut.WriteLine(strComputer & "," & strStatus)
loop

function fPingTest( strComputer )
        dim objShell,objPing
        dim strPingOut, flag
        set objShell = CreateObject("Wscript.Shell")
        set objPing = objShell.Exec("ping " & strComputer)
    strPingOut = objPing.StdOut.ReadAll
    if instr(LCase(strPingOut), "reply") then
           flag = TRUE
        else
           flag = FALSE
        end if
        fPingTest = flag
end function
Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

1

You can use this function IsOnLine :


dim strInputPath, strOutputPath, strStatus
dim objFSO, objTextIn, objTextOut

strInputPath = "C:\serverlist.txt"
strOutputPath = "C:\output.csv"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objTextIn = objFSO.OpenTextFile( strInputPath,1 )
set objTextOut = objFSO.CreateTextFile( strOutputPath )
objTextOut.WriteLine("computer,status")

Do until objTextIn.AtEndOfStream = True
    strComputer = objTextIn.ReadLine
        if IsOnLine(strComputer) then
             strStatus = "UP"
        else
             strStatus = "DOWN"
        end if
        objTextOut.WriteLine(strComputer & "," & strStatus)
loop
'---------------------------------------------------------------------------------------
Function IsOnLine(strHost)
Dim objPing,objRetStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strHost & "'")
For Each objRetStatus In objPing
    If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then
        IsOnLine = False
    Else
        IsOnLine = True
    End If
Next
End Function
 '---------------------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70