I'm having a really hard time with what should be a simple VBscript. I'm trying to do some AD work using VBscript, and ran into an issue with the LDAP limit of 1000 records. For the life of me I cannot resolve it.
I've seen many posts with varying ways to accomplish it, and most of them have an objCommand object, and they set the objCommand.Property("Page Size") value.
Unfortunately, I needed to do some sorting on the DN of an object and for reasons unknown to me, ADO cannot sort properly on the distinguishedName attribute if using the adUseServer CursorLocation. The code I found and ended up using broke away from the objCommand object altogether and ended up with a separate objConnection and objRecordset objects. (There's just way too many ways to create objects with ADO).
At any rate, my objRecordset has a page size property, but setting it seems to do no good. What am I missing here? Using the code attached to export just all users and display their samAccountName along with a counter shows the script bombing after record 999.
I'd appreciate any kind of help to explain to me what is going on. This is driving me batty.
*note* For some reason, my sorting isn't working now either! I had to comment it out of the code in order to do this testing. I'd like to know why I can't run the sort either (it works in another script!)
Const adUseClient = 3
Const adUseServer = 2
Const adCmdText = 1
Const adSecureAuthentication = 1
Const adLockReadOnly = 1
sDNSDomain = "MyDomain.Net"
sADFilter = "(&(objectClass=User)(ObjectCategory=Person))"
sADProperties = "samAccountName,distinguishedName"
sADSearchScope = "subtree"
sADQuery = "<LDAP://" & sDNSDomain & ">;" & sADFilter & ";" & sADProperties & ";" & sADSearchScope
Set oConn = CreateObject("ADODB.Connection")
oConn.Properties("Encrypt Password") = True
oConn.Properties("ADSI Flag") = adSecureAuthentication
oConn.Open "Active Directory Provider"
Set oRecordSet = CreateObject("ADODB.Recordset")
oRecordSet.CursorLocation = adUseClient
'oRecordSet.Sort = "distinguishedName" '<----generates error: Microsoft Cursor Engine: Data provider or other service returned an E_FAIL status.
oRecordSet.PageSize = 10000 '<----tried all kinds of values, e.g., 10, 100, 1000, 10000
oRecordSet.Open sADQuery, oConn, , , adCmdText
iCounter = 0
Do Until oRecordset.EOF
Wscript.Echo iCounter & vbtab & oRecordset.Fields("samAccountName")
iCounter = iCounter + 1