home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / ADSDK.ZIP / Samples / General / ADOQuery / adoquery.vbs
Encoding:
Text File  |  1999-03-16  |  4.1 KB  |  105 lines

  1. /----------------------------------------------------------------------------
  2. //
  3. //  Microsoft Active Directory 2.5 Sample Code
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1996-1999
  6. //
  7. //  File:       adoquery.vbs
  8. //
  9. //
  10. //----------------------------------------------------------------------------
  11.  
  12.  
  13. Public Const ADS_SCOPE_BASE = 0
  14. Public Const ADS_SCOPE_ONELEVEL = 1
  15. Public Const ADS_SCOPE_SUBTREE = 2
  16.  
  17. '--------------------------------------------------------------------------------
  18. ' ADO Query
  19. '------------------------------------------------------------------------------
  20.  
  21.     
  22.     Set con = CreateObject("ADODB.Connection")
  23.     Set com =   CreateObject("ADODB.Command")
  24.     
  25.     'Open a Connection object
  26.     con.Provider = "ADsDSOObject"
  27.     
  28.     '---------------------------------------------------------------------
  29.     ' If you want to be authenticated other than current logged on user
  30.     ' use connection properties of User ID and Password
  31.     '---------------------------------------------------------------------
  32.     'con.Properties("User ID") = "DOMAIN\UserName"
  33.     'con.Properties("Password") = "passwordHere"
  34.     'con.Properties("Encrypt Password") = True  
  35.  
  36.     '--- For anonymous authentication-----
  37.     '---
  38.     'con.Properties("User ID") = ""
  39.     'con.Properties("Password") = ""
  40.     'con.Properties("Encrypt Password") = False
  41.      
  42.  
  43.     
  44.     
  45.     '------------------------------------
  46.     ' Open the connection
  47.     '-------------------------------------
  48.     con.Open "Active Directory Provider"
  49.     
  50.     ' Create a command object on this connection
  51.     Set Com.ActiveConnection = con
  52.     
  53.     '--------------------------------------------------
  54.     ' set the query string
  55.     '---------------------------------------------------
  56.     ' For non Active Directory , you should use LDAP://<yourServer>/DN 
  57.     ' For NT 4.0 and Win9x without DS client packate, you must also specify the LDAP://<serverName>/DN
  58.     ' Comment out the replace adDomainPath with the new value
  59.     ' for example: Com.CommandText = "select name from 'LDAP://ntdsdc1/dc=ntdev,dc=microsoft,dc=com' where objectClass='*'"
  60.     ' Exchange Example:"select name from 'LDAP://exchsrv1/O=Microsoft' where objectClass='*'"
  61.     
  62.    
  63.     adDomainPath = "LDAP://yourServer/DC=ArcadiaBay,DC=com"
  64.     Com.CommandText = "select name from '" & adDomainPath & "' where objectClass='*' "  
  65.     'Note: You may also specify the sort, eg. ORDER BY NAME ( after where clause )
  66.     
  67.     '---------------------------------------------------
  68.     ' Or you can use LDAP Dialect, for example,
  69.     '---------------------------------------------------
  70.     ' Ex Com.CommandText = "<LDAP://ntdsdc1/dc=NTDEV,DC=microsof,DC=com>;(objectClass=*);name"
  71.     ' For LDAP Dialect, the valid search scope are base, oneLevel and subtree
  72.     ' Com.CommandText = "<" & adDomainPath & ">;(objectClass=*);name;subtree"
  73.     
  74.     '-----------------------------------------
  75.     'Set the preferences for Search
  76.     '--------------------------------------
  77.     Com.Properties("Page Size") = 1000
  78.     Com.Properties("Timeout") = 30 'seconds
  79.     Com.Properties("searchscope") = ADS_SCOPE_SUBTREE 'Define in ADS_SCOPEENUM
  80.     Com.Properties("Cache Results") = False ' do not cache the result, it results in less memory requirements
  81.     
  82.     '-----------------------------------------------------------------------------------
  83.     ' For LDAP Dialect (<LDAP:...>), there is no way to specify sort order in the string,
  84.     ' Hoever, you can use this SORT ON property to specify sort order.
  85.     ' for SQL Dialect you can use ORDER BY in the SQL Statement
  86.     ' Ex. Com.Properties("Sort On") = "Name"
  87.     
  88.     '--------------------------------------------
  89.     'Execute the query
  90.     '--------------------------------------------
  91.     Set rs = Com.Execute
  92.         
  93.     
  94.     '--------------------------------------
  95.     ' Navigate the record set
  96.     '----------------------------------------
  97.     rs.MoveFirst
  98.     
  99.     While Not rs.EOF
  100.         wscript.echo Name, " = ", rs.Fields("Name").Value
  101.         rs.MoveNext
  102.     Wend
  103.         
  104.  
  105.