Skip to content

Instantly share code, notes, and snippets.

@rob-murray
Last active December 10, 2015 20:58
Show Gist options
  • Save rob-murray/4491737 to your computer and use it in GitHub Desktop.
Save rob-murray/4491737 to your computer and use it in GitHub Desktop.
List All XP Computer Accounts in Active Directory and write to text file
'===================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.0
'
' NAME: AD Computer search.vbs
'
' AUTHOR: Robert Murray
' DATE : 29/10/2008
'
' COMMENT: List All XP Computer Accounts in Active Directory and write to text file
'
'=======================================
'Def vars
Dim count, myfilename, temp
Dim arrNames()
Const ADS_SCOPE_SUBTREE = 2
'File name where list will be output
myfilename = "c:\tempcom_list.txt"
forReading = 1: forWriting = 2: ynCreate = 1
'Set objects
'==========================================
set fso = CreateObject("Scripting.FileSystemObject")
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
'=========================================
'AD Search
objCommand.CommandText = _
"Select Name from 'LDAP://DC=DOMAIN,DC=XX,DC=XX' " & _
"Where objectClass='computer' AND (operatingSystem = 'Windows 2000 Professional' " & _
"OR operatingSystem = 'Windows XP Professional') ORDER BY Name"
'Alternatively search for servers - 2000 & 2003
'objCommand.CommandText = _
' "Select Name from 'LDAP://DC=XXXXX,DC=XXX,DC=XX' " & _
' "Where objectClass='computer' AND (operatingSystem = 'Windows 2000 Server' " & _
' "OR operatingSystem = 'Windows Server 2003') ORDER BY Name"
'AD search properties
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
count = 0
'========================================
'open file for writing
set myTextStream = fso.OpenTextFile(myfilename,forWriting,ynCreate)
' 'loop through records
Do Until objRecordSet.EOF
'if name is not blank
If IsNull(objRecordSet.Fields("Name").Value) Then
'then move onto next record
bjRecordSet.MoveNext
Else
'else add to array
ReDim Preserve arrNames(count)
arrNames(count) = "Computer Name: " & objRecordSet.Fields("Name").Value & vbcrlf
objRecordSet.MoveNext
count = count + 1
End If
Loop
'Loop through array and output to file -> In array if we want to do anything else with data
For Each strName in arrNames
myTextStream.Write strName
Next
'write total count
myTextStream.Write "Number of comps: " & count & vbcrlf & temp
'close file
myTextStream.Close
'Display simple message box to say done
temp = "Done - Saved file: " & myfilename
MsgBox temp
'END
'========================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment