Const ADS_UF_ACCOUNTDISABLE = 2
' Get OU
strOU = "OU=Users,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
' Execute command to get all users in OU
objCommand.CommandText = _
"<LDAP://" & strOU & ">;" & _
"(&(objectclass=user)(objectcategory=person));" & _
"adspath,distinguishedname,sAMAccountName,displayName,userAccountControl;subtree"
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 & "AD_OU_Export.csv"
Set ExportFile = objFSO.OpenTextFile(strExportFileName, 2, True)
' Show info for each user in OU
Do Until objRecordSet.EOF
intUAC = objRecordset.Fields("userAccountControl")
If intUAC AND ADS_UF_ACCOUNTDISABLE Then
' User is disabled
Else
' Export required info for a user
ExportFile.WriteLine(objRecordSet.Fields("sAMAccountName").Value & ";" & objRecordSet.Fields("displayName").Value & ";" & objRecordSet.Fields("distinguishedname").Value)
End If
' 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"