WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADO DAO
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Retrieve all user objects in a domain via ADO

 Keywords:  Retrieve all user objects domain ADO

QUESTION:

Does anyone have a code fragment that shows how to retrieve all user objects in a given domain in an AD forest?

Here is a winbatch script that I wrote using ADO. I am unable to set the active connection and how do you set the page size assuming there is a object limit.?

cnn = objectopen("ADODB.Connection")
uid = ""
pwd = ""
adStateOpen = 1
adfldmaybenull = 64

CNN.Provider = "ADSDSOObject"
cnn.Open("ADs Provider", uid, pwd)


;It FAILS at the line below <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;you have to assign the command object the above cnn connection. Then you can set the page size... 
;since the below line fails then there is no way to set the command object to that connection and thus no page size.
oCmd.ActiveConnection = cnn 



;strAttributes = "ADsPath,name,distinguishedName,cn,givenName,sn"
strAttributes = "ADsPath,name"
strFilter = ""

aDSPATH=";"
SEARCH=strcat("(&(objectCategory=person)(objectClass=user)", strFilter , ");", strAttributes,";subtree")
message("",search)

;is the below line correct??????
;how do you set the page size???????!!!
;cnn.Properties("Page Size") = "1"
;cnn.properties.pagesize=1000

rs = cnn.execute(strcat(adspath,search))

msgstringhan=rs.Fields(0)
namnamehan=rs.Fields(1)
x=0
userlisttabstring=""

while !rs.eof
   x=x+1
   vaLUE1=msgstringhan.VALUE
   value2=namnamehan.value
   name=value2
   userlisttabstring=ItemInsert(name, -1, userlisttabstring, @tab)
   rs.movenext
endwhile


;userlisttabstring=itemsort(userlisttabstring,@tab)
message("Done!",strcat("Records Found=",x,@crlf,"UserNameTablistBytesUsed=",strlen(userlisttabstring)))
rs.close
cnn.close
exit

The following is from the wwwbatch.ini

[OLE Exception]
Provider=The size limit for this request was exceeded.


[OLE DLL]
Function=Invoke (2)
ErrorCode=9 (0x80020009)
ErrorDesc=Exception occurred.

MS says that there is a limit for that provider...they say you can correct it by setting the page size count?? How the heck do you do that in winbatch. I also have to assign the command object the cnn connection object.

ANSWER:

First, the only way found to assign the command object (using the ActiveConnection property) to the connection object, is as follows:
oCmd.ActiveConnection = cnn.ConnectionString 
As for setting the page size, that can be handled as follows:
pgsize=ocmd.Properties("Page Size")
pgsize.value=999
Here is the entire code that should work...
filename2="d:\winbatchdata\ADSIuserlist.txt"
filepointer=fileopen(filename2,"write")
filewrite(filepointer,"")
fileclose(filepointer)

cnn = objectopen("ADODB.Connection")
oCmd=objectopen("ADODB.Command")
uid = ""
pwd = ""
adStateOpen = 1
adfldmaybenull = 64

CNN.Provider = "ADSDSOObject"
cnn.Open("ADs Provider")

oCmd.CommandType = 1 ;adCmdText
;' Find out if the attempt to connect worked.
If cnn.State == adStateOpen

oCmd.ActiveConnection = cnn.ConnectionString ;<<<<<<<<<<<<<<< define active connection

BOXOPEN("ADSI ADO TEST","Please Wait...")
;strAttributes = "ADsPath,name,distinguishedName,cn,givenName,sn"
strAttributes = "ADsPath,name"
strFilter = ""

aDSPATH=";"
SEARCH=strcat("(&(objectCategory=person)(objectClass=user)", strFilter , ");", strAttributes,";subtree")

message("",search)

pgsize=ocmd.Properties("Page Size");<<<<<<<<<<<<<<< 
pgsize.value=999                   ;<<<<<<<<<<<<<<< Set Page Size

oCmd.CommandText =strcat(adspath,search)
rs=ocmd.execute
msgstringhan=rs.Fields(0)
namnamehan=rs.Fields(1)
x=0
totaltime=0
userlisttabstring=""
pageflipindex=0

gosub timestart

while !rs.eof
  x=x+1
  vaLUE1=msgstringhan.VALUE
  value2=namnamehan.value
  name=value2
  gosub writeuser
  BOXTEXT(STRCAT(VALUE1,@CRLF,name,@crlf,"RecordNumber=",x))
  userlisttabstring=ItemInsert(name, -1, userlisttabstring, @tab)
  rs.movenext
  pageflipindex=pageflipindex+1
endwhile

gosub timecal

boxtext(strcat("Complete!"))
boxshut()
message("Done!",strcat("Records Found=",x,@crlf,"UserNameTablistBytesUsed=",strlen(userlisttabstring),@crlf,"Runtime in Minutes=",totaltime/60))
rs.close
cnn.close
endif
exit

:timestart
starttime=TimeYmdHms()
return

:timecal
endtime=TimeYmdHms()
copytime=TimeDiffSecs(endtime,starttime)
totaltime=totaltime+copytime
return

:writeuser
filename2="d:\winbatchdata\ADSIuserlist.txt"
filepointer=fileopen(filename2,"append")
filewrite(filepointer,value1)
fileclose(filepointer)
return 

Article ID:   W15242
File Created: 2002:09:05:13:50:46
Last Updated: 2002:09:05:13:50:46