【发布时间】:2017-01-24 00:54:57
【问题描述】:
以下 VBScript 将从主机名列表到 Excel 电子表格中显示单个 ping 命令的状态:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
'# Define Labels
objExcel.Cells(1, 1).Value = "Node"
objExcel.Cells(1, 2).Value = "Status"
'# Create file system object for reading the hosts from text file
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("MachineList.Txt")
'# Loop thru the text file till the end
Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine
'# Create shell object for Pinging the host machines
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
objExcel.Cells(intRow, 1).Value = HostName
'# use switch case for checking the machine updown status
Select Case Ping
Case 0
objExcel.Cells(intRow, 2).Value = "Up"
objExcel.Cells(intRow, 2).Interior.Color = vbGreen
Case 1
objExcel.Cells(intRow, 2).Value = "Down"
objExcel.Cells(intRow, 2).Interior.Color = vbRed
End Select
intRow = intRow + 1
Loop
'# Format the excel
objExcel.Range("A1:B1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
此代码按预期工作。
我想通过 Excel 的实时更新不断地 ping 主机名。我尝试将-t 添加到 ping 命令中 - 而不是像在命令提示符中那样添加-n 1,但脚本会挂起,因为它会在列表中的第一个主机名处停止,并且不会进一步。
我的问题:如何实现连续ping命令或重复运行脚本?
在网上找了几天解决办法,没找到。
【问题讨论】:
-
您可以在 Excel 中使用
Application.OnTime以特定时间间隔循环运行宏。编辑:由于您使用的是 vbscript,您可以将代码放入循环中并使用WScript.Sleep()添加延迟虽然您需要更改管理结果显示的方式... -
@TimWilliams,您能否详细介绍一下循环概念以及我们在这里讨论的结果显示有多少变化?在这条开发道路上,我一直在想出一个想法,将输出变成一个 HTML 文件。我知道这个问题有点偏离主题,但如果我必须转向,这就是我想进入的方向 - 就像一个注释一样。