on error resume next if wscript.arguments.count <> 3 then wscript.echo "query.vbs ldap command-line queries by Ben Herman (http://www.benherman.com)" wscript.echo wscript.echo "Usage:" wscript.echo wscript.echo "cscript query.vbs server ldapqry returnAttr" wscript.echo "(i.e. cscript query.vbs exchange_server st=NY rfc822mailbox" wscript.echo " or: cscript query.vbs e2kserver msExchIMAddress=* mail" wscript.echo " or: cscript query.vbs cn=Users,dc=domain,dc=com ""(!(employeeid=*))"" cn" wscript.echo wscript.echo "See RFC 2254 for LDAP query search filters." wscript.quit end if strServer=wscript.arguments(0) strQuery=wscript.arguments(1) strReturnAttr=wscript.arguments(2) arrReturn=split(strReturnAttr,",") intReturn=ubound(arrReturn) 'Echo out header row strTemp="""" for intI=0 to ubound(arrReturn) if len(strTemp)>1 then strTemp=strTemp&",""" strTemp=strTemp&arrReturn(intI)&"""" next 'wscript.echo strTemp 'Run LDAP query wscript.echo "Running LDAP Query ;("&strQuery&");"&strReturnAttr&";SubTree" 'wscript.echo 'wscript.echo 'Const ADS_SCOPE_BASE = 0 'Const ADS_SCOPE_ONELEVEL = 1 'Const ADS_SCOPE_SUBTREE = 2 'Const ADS_CHASE_REFERRALS_NEVER = 0 ADS_CHASE_REFERRALS_SUBORDINATE = &H20 ADS_CHASE_REFERRALS_EXTERNAL = &H40 ADS_CHASE_REFERRALS_ALWAYS = ADS_CHASE_REFERRALS_SUBORDINATE Or ADS_CHASE_REFERRALS_EXTERNAL Set objConn = WScript.CreateObject("ADODB.Connection") Set objRS = WScript.CreateObject("ADODB.Recordset") Set objCommand = WScript.CreateObject("ADODB.Command") objConn.Provider = "ADsDSOObject" objConn.Open "Active Directory Provider" Set objCommand.ActiveConnection = objConn objCommand.CommandText = ";("&strQuery&");"&strReturnAttr&";SubTree" 'objCommand.Properties("Page Size") = 99 objCommand.Properties("Chase referrals") = ADS_CHASE_REFERRALS_ALWAYS Set objRS = objCommand.Execute if objRS.recordcount<1 then wscript.echo "No addresses found. Exiting..." wscript.quit end if objRS.movefirst While Not objRS.EOF strTemp="" for intI = 0 to intReturn sNext="" if len(strTemp)> 0 then strTemp=strTemp&"," if TypeName(objRS.Fields(intI).Value)="Variant()" then aNextTmp=objRS.Fields(intI).Value if ubound(aNextTmp) = 1 then sNext=aNextTmp(0) elseif ubound(aNextTmp) > 1 then sNext=aNextTmp(0) for iVarIndex=1 to ubound(aNextTmp) sNext=sNext&"%"&aNextTmp(iVarIndex) next end if else sNext=objRS.Fields(intI).Value end if if len(sNext)>0 then strTemp=strTemp&""""&CSTR(sNext)&"""" next wscript.echo strTemp objRS.MoveNext Wend