Use a VBScript script to ping a machine(转载)

上一篇 / 下一篇  2009-10-20 14:44:27 / 个人分类:VBScript脚本

The Windows Management Instrumentation (WMI) classes in Windows XP and later provide a Win32_PingStatus object that you can use to ping a machine. The following script, which you can download athttp://www.windowsitpro.com/articles/download/vbpinging.zipuses this object to ping a passed hostname or IP address. Because of space constraints, some lines wrap to two lines.

Option Explicit

Dim strHost

' Check that all arguments required have been passed.
If Wscript.Arguments.Count < 1 Then
Wscript.Echo "Arguments <Host> required. For example:" & vbCrLf _
& "cscript. vbping.vbs savdaldc01"
Wscript.Quit(0)
End If

strHost = Wscript.Arguments(0)

if Ping(strHost) = True then
Wscript.Echo "Host " & strHost & " contacted"
Else
Wscript.Echo "Host " & strHost & " could not be contacted"
end if

Function Ping(strHost)

dim objPing, objRetStatus

set bjPing = 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
Ping = False
'WScript.Echo "Status code is " & objRetStatus.StatusCode
else
Ping = True
'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
end if
next
End Function

You can modify this script. to do whatever you need. Notice that I've commented out some lines (') that give more information about the ping attempt, but you can leave the lines in if the information would be useful to you. Run the script. by using the following command:

D:\projects\VBScripts>cscript. vbping.vbs savdalex01
which will give the following sample output:

Host savdalex01 contacted

You can find more information about Win32_PingStatus at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp .

URL:http://windowsitpro.com/article/articleid/48449/how-can-i-use-a-vbscript-script-to-ping-a-machine.html

国内实例

要添加计算机的话按照第二行的格式添加即可
还有就是把strOutputFilePath改成你自己的路径

  1. arrComputers = Array(_
  2. "127.0.0.1",_
  3. "172.16.100.4",_
  4. "172.16.100.6")
  5. strDate = Year(Date) & Month(Date) & Day(Date)
  6. strOutputFilePath = "e:\CheckResult - " & strDate & ".txt"

  7. Set bjFSO = CreateObject("Scripting.FileSystemObject")
  8. Set bjFile = objFSO.CreateTextFile(strOutputFilePath)
  9. For Each objComputer In arrComputers
  10.         If Ping(objComputer) = True Then
  11.                 objFile.WriteLine objComputer & " : UP"
  12.         Else
  13.                 objFile.WriteLine objComputer & " : DOWN"
  14.         End If
  15. Next
  16. objFile.Close

  17. Function Ping(strComputer)
  18.         Set bjWMIService = GetObject("winmgmts:\\.\root\cimv2")
  19.         Set colItems = objWMIService.ExecQuery("Select * From Win32_PingStatus Where Address='" & strComputer & "'")
  20.         For Each objItem In colItems
  21.                         Select case objItem.StatusCode
  22.                                 Case 0
  23.                                         Ping = True
  24.                                 Case Else
  25.                                         Ping = False
  26.                         End select
  27.                 Exit For
  28.         Next
  29. End Function

URL:http://bbs.winos.cn/thread-79977-1-1.html


TAG:

 

评分:0

我来说两句

Open Toolbar