我正在尝试修改现有的VBscript,它从我的不同站点中提取服务器名称,然后ping它们,服务器全部按照它们所在的站点组合在一起。然后,该脚本在Excel页面中显示服务器是联机还是脱机。除了脚本试图ping该站点的名称之外,这一切都正常。这里的目标是主要通过在excel文件中显示站点名称但不进行ping操作来清理我的结果。 这是我目前的VBscript
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "On Line"
objExcel.Cells(1, 3).Value = "Off Line"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("servers.txt")
Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
objExcel.Cells(intRow, 1).Value = HostName
Select Case Ping
Case 0 objExcel.Cells(intRow, 2).Value = "On Line"
Case 1 objExcel.Cells(intRow, 3).Value = "Off Line"
End Select
intRow = intRow + 1
Loop
objExcel.Range("A1:B1:C1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
这是文本文档的副本。
DISTRICT OFFICE
Server A
Server B
LAB
Server LA
School 1
Server 1A
Server 1B
School 2
Server 2A
Server 2B
School 3
Server 3A
Server 3B
Server 3c
答案 0 :(得分:0)
我可能只是在文本文件中的所有组名前面添加一些任意字符,然后使用if语句确保您正在阅读的文本文件中的行没有字符。
例如,如果我在文本文件中放置了~
组名称,我可以在代码中添加以下更改以过滤掉组名称:
...
Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine
If Not (Mid(HostName, 0, 1) = "~") Then 'Checks the first character of the current line
Set WshShell = WScript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
objExcel.Cells(intRow, 1).Value = HostName
Select Case Ping
Case 0 objExcel.Cells(intRow, 2).Value = "On Line"
Case 1 objExcel.Cells(intRow, 3).Value = "Off Line"
End Select
End If
intRow = intRow + 1
Loop
...
...
表示省略了与示例无关的代码。
答案 1 :(得分:0)
另一个更简单的实现方式是:
... OTHER CODE ...
HostName = InputFile.ReadLine
IF NOT(UCase(HostName) = HostName) THEN
Set WshShell = Wscript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
objExcel.Cells(intRow,1).Value = HostName
Select Case Ping
Case 0 : objExcel.Cells(intRow, 2).Value = "On Line"
Case 1 : objExcel.Cells(intRow, 3).Value = "Off Line"
End Select
End If
... OTHER CODE ...
要使用此代码,您需要做的只是大写您的组标签。如果您想获得幻想,可以将它们包含在电子表格中:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Group"
objExcel.Cells(1, 2).Value = "Machine Name"
objExcel.Cells(1, 3).Value = "On Line"
objExcel.Cells(1, 4).Value = "Off Line"
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("servers.txt")
Do While Not (InputFile.atEndOfStream)
HostName = InputFile.ReadLine
IF NOT(UCase(HostName) = HostName) THEN
Set WshShell = Wscript.CreateObject("WScript.Shell")
Ping = WshShell.Run("ping -n 1 " & HostName, 0, True)
objExcel.Cells(intRow,1).Value = GroupName
objExcel.Cells(intRow,2).Value = HostName
Select Case Ping
Case 0 : objExcel.Cells(intRow, 3).Value = "On Line"
Case 1 : objExcel.Cells(intRow, 4).Value = "Off Line"
End Select
Else
GroupName = HostName
End If
intRow = intRow + 1
Loop
objExcel.Range("A1:B1:C1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit