2015-10-14 38 views
0

我试图修改一个现有的VBscript,从我的不同站点从服务器名称从文本文件中拉出来,然后ping它们,服务器都被网站分组在一起他们位于。该脚本会在Excel页面中显示服务器是联机还是脱机。这一切都可以正常工作,但脚本也尝试ping站点的名称。这里的目标是主要通过在excel文件中显示网站名称来清理我的结果,但不会被ping通。 这里是我当前的VBScriptVBS ping一组服务器但ping不通群名

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

你的'servers.txt'文件在哪里?那包括网站吗? – Marc

+0

我编辑了问题以包含文本文档的副本。这是否有帮助? –

+0

想要ping的所有行条目都以“School”开始? – Sorceri

回答

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 
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 

... 

...指示省略了与示例无关的代码。