用于 ping 计算机列表的 VBscript
VBscript to ping a list of computers
我正在使用此 VBscript
代码在记事本中 ping 计算机列表并将 ping 结果存储在 csv 文件中。
存储在记事本文件中的所有计算机现在都可用(cmd >>> ping servername
),但 csv 文件中的结果都是 DOWN。
我试过也存储在记事本文件中一台电脑不可用,但是在csv文件中ping的结果都是DOWN...
有什么建议吗?
下面是我的代码
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
你可以使用这个 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
'---------------------------------------------------------------------------------------
我正在使用此 VBscript
代码在记事本中 ping 计算机列表并将 ping 结果存储在 csv 文件中。
存储在记事本文件中的所有计算机现在都可用(cmd >>> ping servername
),但 csv 文件中的结果都是 DOWN。
我试过也存储在记事本文件中一台电脑不可用,但是在csv文件中ping的结果都是DOWN...
有什么建议吗?
下面是我的代码
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
你可以使用这个 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
'---------------------------------------------------------------------------------------