Printout Header
RSS Feed

First Steps: Basic Script Examples


Imagine you are the system admin of the Active Directory domain 'example.com'. You want to retrieve information concerning objects in the Active Directory from the domain controller 'DC1' - if possible via script. And maybe you even want to alter objects and their attributes.


This is quite easy with the Active Directory Services Interface. Please have a look at the following script examples. If you don't know how Visual Basic scripts work, you could read the introduction for writing and running VBScripts here in the SlefADSI Tutorial first.



Example 1


First of all we want to have displayed all objects of the OU 'production':


Set ou = GetObject("LDAP://dc1.example.com/ou=Fabrikation,dc=example,dc=com") For Each obj In ou 'For all objects of the OU WScript.Echo obj.name '...show object name Next

Are you confused about the function GetObject with which we have accessed the AD directory? Then you could read more information about 'Establishing a connection to the directory' in the SelfADSI tutorial.


Or maybe you have got problems with the curios pathname LDAP://dc1.example.com/ou0Fabrikation,dc=example,dc=com? Then you could read the topic 'LDAP Pathnames - Distinguished Names' in the SelfADSI tutorial.



Example 2


Let's refine our example. Now we want to see all display names of user within the OU:


Set ou = GetObject("LDAP://dc1.example.com/ou=Fabrikation,dc=example,dc=com") For Each obj In ou 'For all objecte of the OU... If (obj.Class = "user") Then 'For user only... WScript.Echo obj.displayName '...show the displayName WScript.Echo obj.mail '...and the mail address End If Next

You wonder how to find out the names of attributes? Please read the detailed description of the attribute 'Class' for user objects. The description of all attributes of the object classes AD User, AD Contact, AD Group and Exchange 5.5 Mailbox are available in the SelfADSI tutorial.



Example 3


Another situation: For testing purposes you want to create 500 user in the OU 'Test' fast and easily. The following script will do that for you:


Set ou = GetObject("LDAP://dc1.example.com/ou=Test,dc=example,dc=com") For i = 1 To 500 '500x ... set user = ou.Create("user", "cn=Benutzer" & i) '... create a user user.sAMAccountName = "Benutzer" & i 'set log on name user.displayName = "Benutzer" & i 'set displayName user.SetInfo 'write data to the server Next

Detailed user instruction concerning creating diverse types of objects can be found in the paragraph 'Creating directory objects' here in this tutorial.