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改成你自己的路径
- arrComputers = Array(_
- "127.0.0.1",_
- "172.16.100.4",_
- "172.16.100.6")
- strDate = Year(Date) & Month(Date) & Day(Date)
- strOutputFilePath = "e:\CheckResult - " & strDate & ".txt"
- Set bjFSO = CreateObject("Scripting.FileSystemObject")
- Set bjFile = objFSO.CreateTextFile(strOutputFilePath)
- For Each objComputer In arrComputers
- If Ping(objComputer) = True Then
- objFile.WriteLine objComputer & " : UP"
- Else
- objFile.WriteLine objComputer & " : DOWN"
- End If
- Next
- objFile.Close
- Function Ping(strComputer)
- Set bjWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * From Win32_PingStatus Where Address='" & strComputer & "'")
- For Each objItem In colItems
- Select case objItem.StatusCode
- Case 0
- Ping = True
- Case Else
- Ping = False
- End select
- Exit For
- Next
- End Function
URL:http://bbs.winos.cn/thread-79977-1-1.html
收藏
举报
TAG: