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.

Const ADS_SCOPE_SUBTREE = 2
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
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
    LogInfo objRecordSet.Fields("Name").Value
    objRecordSet.MoveNext
Loop

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
 Else
  Set objFile = fso.CreateTextFile(objFile, True)
  objFile.WriteLine strResult
 End If
 objFile.close
 Err.clear
End Sub

 

Add a Comment