2015-10-14 88 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 

... 

...指示省略了與示例無關的代碼。