Ying Li at myITforum.com

PowerShell & System Center

VB Script to Query a Particular Entry in Hosts file on Multiple Machines

Scenario – Some time when you manage multiple servers and you want to verify if a particular entry(SearchString) exists in Hosts File…

'On Error Resume Next

Const ForReading = 1
SearchString = Inputbox ("Enter the Entry Value")

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2

objExcel.Cells(1, 1).Value = "Server Name"
objExcel.Cells(1, 2).Value = "Target Entry"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set InputFile = objFSO.OpenTextFile("MachineList.Txt")
Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine

objExcel.Cells(intRow, 1).Value = strcomputer

GetHostsEntry

intRow = intRow + 1

Loop

objExcel.Range("A1:E1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit

Wscript.Echo "Done"

'************************************************************************************************

Sub GetHostsEntry

 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("\\" & strComputer & _
  "\c$\WINDOWS\system32\drivers\etc\hosts")

If objFSO.FileExists(objFile) Then
Set objHostsFile = objFSO.OpenTextFile(objFile, ForReading)
Do Until objHostsFile.AtEndOfStream
strEntry = objHostsFile.Readline

SearchEntry = InStr(strEntry , SearchString)
   If SearchEntry > 0 Then
   strCurEntry = strEntry
  
   objExcel.Cells(intRow, 2).Value = strCurEntry
  
   End If

Loop

   objHostsFile.Close

Else

objExcel.Cells(intRow, 2).Value = "Hosts File Not Exist"

End If

End Sub

Posted: Dec 10 2007, 09:21 PM by yli628 | with no comments
Filed under:

Comments

No Comments