'****************************************************************
' Filename..: enum_ad_host_files.vbs
' Author....: David M. Stein
' Date......: 04/15/2012
' Purpose...: search for hosts files with custom entries
'****************************************************************
dns_netbios = "short_name_of_your_active_directory_domain"
Const ForReading = 1
Const ForWriting = 2
wscript.echo "info: scanning domain = " & dns_netbios
Set objDom = GetObject( "WinNT://" & dns_netbios )
Set objFSO = CreateObject("Scripting.FileSystemObject")
tcount = 0
For each obj in objDom
If Lcase(obj.Class) = "computer" Then
computerName = obj.Name
wscript.echo "info: " & computerName
tcount = tcount + 1
CheckHosts computerName
End If
Next
Sub CheckHosts(cn)
Dim filename, objFile, strLine, found
filename = "\\" & cn & "\admin$\system32\drivers\etc\hosts"
wscript.echo "info: searching for: " & filename
If objFSO.FileExists(filename) Then
On Error Resume Next
Set objFile = objFSO.OpenTextFile(filename, ForReading)
If err.Number = 0 Then
Do Until objFile.AtEndOfStream
strLine = Trim(objFile.Readline)
If Left(strLine,1) <> "#" And strLine <> "" Then
found = True
End If
Loop
objFile.Close
If found = True Then
wscript.echo "info: custom entry found!"
Else
wscript.echo "info: no custom entries found."
End If
Else
wscript.echo "fail: error (" & err.Number & ") = " & err.Description
End If
Else
wscript.echo "fail: unable to locate hosts file on " & cn
End If
End Sub
wscript.echo "info: " & tcount & " account objects found"
Showing posts with label networking. Show all posts
Showing posts with label networking. Show all posts
Sunday, April 15, 2012
Query AD Computers with Custom HOSTS File Entries
Labels:
active directory,
adsi,
files,
inventory,
networking,
vbscript,
windows
Friday, July 10, 2009
KiXtart: Enumerate HOSTS file Entries
Break ON
$systemRoot = ExpandEnvironmentVars("%systemroot%")
$hostsFile = $systemRoot+"\system32\drivers\etc\hosts"
$fileHandle = FreeFileHandle()
$entries = 0
If Exist($hostsFile)
? "info: hosts file found at "+$hostsFile
If Open($fileHandle, $hostsFile, 1) = 0
? "info: reading file..."
$line = ReadLine($fileHandle)
While @ERROR = 0
If Left($line, 1) <> "#"
; ignore lines beginning with # as they are comments
? "line --> $line"
$entries = $entries + 1
EndIf
$line = ReadLine($fileHandle)
Loop
$=Close($fileHandle)
? "info: $entries entries found"
Else
? "fail: unable to open hosts file!"
EndIf
Else
? "fail: hosts file cannot be found!"
EndIf
Subscribe to:
Posts (Atom)