SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

Targeting Active Directory objects (users, computers, etc)

 Here’s a great example of how to do something to every user, computer, or other object in the directory. This enumerates through every object, recursing the entire directory.

  1. '
  2. ' ScriptingAnswers.com Essentials - by Don Jones
  3. '
  4. ' Targeting AD
  5. ' This shows how to target every user, computer,
  6. ' or OU in AD and do something to each. You
  7. ' just pop your code into the DoObject subroutines.

  8. 'connect to the root of AD
  9. Dim rootDSE, domainObject
  10. Set rootDSE=GetObject("LDAP://RootDSE")
  11. domainContainer = rootDSE.Get("defaultNamingContext")
  12. Set oDomain = GetObject("LDAP://" & domainContainer)

  13. 'start with the domain root
  14. WorkWithObject oDomain

  15. Sub DoObject_User(strName)
  16. 'your code goes here - strName
  17. 'is a user name. If you don't care
  18. 'about users, just leave this empty.
  19. End Sub

  20. Sub DoObject_Computer(strName)
  21. 'your code goes here - strName
  22. 'is a computer name. If you don't care
  23. 'about computers, just leave this empty.
  24. End Sub

  25. Sub WorkWithObject(oContainer)
  26. Dim oADObject
  27. For Each oADObject in oContainer
  28. Select Case oADObject.Class
  29. Case "user"
  30. 'oADObject represents a USER object;
  31. 'do something with it
  32. DoObject_User oADObject.cn
  33. Case "computer"
  34. 'oADObject represents a COMPUTER object;
  35. 'do something with it
  36. DoObject_Computer oADObject.cn
  37. Case "organizationalUnit" , "container"
  38. 'oADObject is an OU or container…
  39. 'go through its objects
  40. WorkWithObject(oADObject)
  41. End select
  42. Next
  43. End Sub

You’ll notice two subroutines, DoObject_User and DoObject_Computer. Just put your code in there. Inside those subroutines, the variable strName contains the name of the current directory object so that you can do something with it.

You can have your script start elsewhere - just change the second LDAP query to something like Set oDomain = GetObject(”LDAP://ou=Sales,dc=domain,dc=com”) and it’ll start with that OU (for example) rather than the root of the domain.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , , ,

Leave a Reply


Entries (RSS) and Comments (RSS).