Const ADS_SCOPE_SUBTREE = 200
strExportFile = "AD_OU_Export_Computers.csv"
strSelectAttr = "cn, operatingSystem, operatingSystemServicePack"
' Get OU
strOU = "OU=Laptops W7,OU=Organisatie,DC=VERZ,DC=LOCAL"
' Create connection to AD
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
' Create command
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
' Execute command to get all users in OU
objCommand.CommandText = _
"Select " & strSelectAttr & " from 'LDAP://" & strOU & "' " _
& "Where objectClass='computer'"
Set objRecordSet = objCommand.Execute
' Create the Export File
Set objFSO = CreateObject("Scripting.FileSystemObject")
strScriptDir = Left(WScript.ScriptFullName, (Len(WScript.ScriptFullName))-(Len(WScript.ScriptName)))
strExportFileName = strScriptDir & strExportFile
Set ExportFile = objFSO.OpenTextFile(strExportFileName, 2, True)
ExportFile.WriteLine("CN;operatingSystem;operatingSystemServicePack")
' Show info for each user in OU
Do Until objRecordSet.EOF
' Export required info for a computer
ExportFile.WriteLine(objRecordSet.Fields("cn").Value & ";" & _
objRecordSet.Fields("operatingSystem").Value & ";" & _
objRecordSet.Fields("operatingSystemServicePack").Value)
' Move to the next user
objRecordSet.MoveNext
Loop
' Close the Export File
ExportFile.Close
' Clean up
objRecordSet.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing
MsgBox "Klaar"