-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumeratehosts.vbs
59 lines (45 loc) · 1.84 KB
/
Enumeratehosts.vbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'Option Explicit
Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
Dim adoRecordset, strComputerDN, strOS
Dim WshShell, curDir, wShell, file
Set wShell = WScript.CreateObject("Shell.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FileSystem.OpenTextFile("Enumhosts.txt",2,True)
' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory for all computers.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
curDir = WshShell.CurrentDirectory
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
strQuery = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer);" & "sAMAccountName,operatingSystem;subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
' Enumerate computer objects with server operating systems.
Do Until adoRecordset.EOF
strOS = adoRecordset.Fields("operatingSystem").Value
If (InStr(UCase(strOS), "XP") > 0) Then
strComputerDN = adoRecordset.Fields("sAMAccountName").Value
OutPutFile.WriteLine Left(strComputerDN,Len(strComputerDN)-1)
End If
adoRecordset.MoveNext
Loop
adoRecordset.Close
OutPutFile.Close
' Clean up.
adoConnection.Close
Set objRootDSE = Nothing
Set adoCommand = Nothing
Set adoConnection = Nothing
Set adoRecordset = Nothing
Set wShell = Nothing
Set WshShell = Nothing
Set FileSystem = Nothing
Set OutPutFile = Nothing