Script to pull Windows Server 2008 servers AD and log to a text file

Here is a script to pull all Windows Server 2008 servers out of your Active Directory and log to a text file.  Just change the LDAP string.

Set fso = Wscript.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
objCommand.CommandText = _
    “SELECT Name FROM ‘LDAP://DC=example,DC=com’ WHERE objectClass=’computer’ ” & _
        “and operatingSystemVersion = ‘6.0 (6001)'” 
objCommand.Properties(“Page Size”) = 1000
objCommand.Properties(“Searchscope”) = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute

Do Until objRecordSet.EOF
    Wscript.Echo “Computer Name: ” & objRecordSet.Fields(“Name”).Value
    LogInfo objRecordSet.Fields(“Name”).Value

set objCommand = Nothing
set objRecordset = Nothing
set objConnection = Nothing

‘ —————————————————————————————
‘ |                                 Loginfo     |
‘ —————————————————————————————

Sub LogInfo(strResult)
 dim objFile, fso2
 objFile = “output.txt”
 If fso.FileExists(objFile) Then
  Set objFile = fso.OpenTextFile(objFile, 8)
  objFile.WriteLine strResult
  Set objFile = fso.CreateTextFile(objFile, True)
  objFile.WriteLine strResult
 End If
End Sub


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: