Tuesday, August 4, 2009

Using VBScript to PLAY with Active Directory

Using VBScript to Export All Objects in the Marketing OU

In this example, you use a text editor such as Notepad to create a VBScript program. The script searches the Marketing OU and creates a text file that lists all of the user objects and a subset of their attributes.

To create the export script

  1. Copy the following text into your text editor:

    'Global variables  Dim oContainer  Dim OutPutFile  Dim FileSystem  'Initialize global variables  Set FileSystem = WScript.CreateObject("Scripting.FileSystemObject")  Set OutPutFile = FileSystem.CreateTextFile("marketing.txt", True)  SetoContainer=GetObject("LDAP://OU=marketing,DC=reskit,DC=com")  'Enumerate Container  EnumerateUsers oContainer  'Clean up  OutPutFile.Close  Set FileSystem = Nothing  Set oContainer = Nothing  WScript.Echo "Finished"  WScript.Quit(0)  Sub EnumerateUsers(oCont)  Dim oUser  For Each oUser In oCont  Select Case LCase(oUser.Class)  Case "user"  If Not IsEmpty(oUser.distinguishedName) Then  OutPutFile.WriteLine "dn: " & oUser.distinguishedName  End If   If Not IsEmpty(oUser.name) Then   OutPutFile.WriteLine "name: " & oUser.Get ("name")  End If  'need to do this because oUser.name would get back the Relative   Distinguished name (i.e. CN=Jo Brown)  If Not IsEmpty(oUser.st) Then   OutPutFile.WriteLine "st: " & oUser.st  End If  If Not IsEmpty(oUser.streetAddress) Then   OutPutFile.WriteLine "streetAddress: " & oUser.streetAddress  End If  Case "organizationalunit" , "container"  EnumerateUsers oUser  End Select  OutPutFile.WriteLine   Next  End Sub 
  2. Save the file as Export.vbs.

  3. At the command prompt type export.vbs and press Enter. This creates a file named Marketing.txt, which contains a list of users and some of their attributes, such as distinguished name, name, state, and street address.

With appropriate modification, this script can be used with any application that supports COM and Visual Basic technologies. Such applications include Microsoft Visual Basic, Microsoft Excel, and Microsoft Access. Scripting can also be hosted by Internet Explorer and Internet Information Services 5.0, which is part of Windows 2000 Server.

Using VBScript to Modify All Objects in the Marketing OU

In this example, the Marketing organization has moved to a new office address. A simple VBScript program is used to perform a batch modification for all user objects in the Marketing organization. The script alters the state, street, locality, and postal code attributes.

  1. Copy the following text into your text editor:

    Dim  oContainer Set  oContainer=GetObject("LDAP://  OU=marketing,DC=reskit,DC=com")  ModifyUsers oContainer  'cleanup  Set oContainer = Nothing  WScript.Echo "Finished"  Sub ModifyUsers(oObject)  Dim oUser oObject.Filter = Array("user") For Each oUser in oObject oUser.Put "st","New York" oUser.Put "streetAddress","825 Eighth Avenue" oUser.Put "postalCode","10019" oUser.Put "l","New York" oUser.SetInfo Next  End Sub 
  2. Save the file as Modify.vbs.

  3. At the command prompt, type modify.vbs and press Enter. This processes all objects in the Marketing organizational unit and modifies all users, altering the state, street address, postal code, and locality attributes.

Using VBScript to Create a User Object in the Marketing OU

In this example, you use VBScript to add a new user to the Marketing organization. This example illustrates how easy it is to use ADSI and VBScript to programmatically access the directory. Note that in this example, only a limited set of attributes are configured during the user creation.

To create the script and add the user

  1. Copy the following text into your text editor:

    Dim oContainer 'Parent container  of new   user Dim  oUser 'Created user  'Get parentcontainerSetoContainer=GetObject("LDAP://OU=marketing,  DC=reskit,DC=com")  'Create user  Set oUser = oContainer.Create("User","CN=Jo Brown")  'Assign properties values to user  oUser.Put "samAccountName","Jo"  oUser.Put "givenName","Jo"  oUser.Put "sn","Brown"  oUser.Put "userPrincipalName","jo@reskit.com"  oUser.SetInfo  'Clean up  Set oUser = Nothing  Set oContainer = Nothing  WScript.Echo "Finished" 
  2. Save the file as Adduser.vbs.

  3. At the command prompt, type adduser.vbs and press Enter. This creates a new user named Jo Brown in the Marketing OU.

Using VBScript to Delete a User

In this example, you use VBScript to delete a user from the Marketing organization.

  1. Copy the following text into your text editor:

    Dim oContainer 'Parent container of object to be  deleted 'Get parent  container Set  oContainer=GetObject("LDAP://OU=marketing,  DC=reskit,DC=com")  'Delete user  oContainer.Delete "user","CN=Jo Brown"  'Clean up  Set oContainer = Nothing  WScript.Echo "Finished" 
  2. Save the file as Deluser.vbs.

  3. At the command prompt, type deluser.vbs and press Enter. This deletes the user Jo Brown from the Marketing OU.
    -------------------
    Thanks,
    http://sccm07.blogspot.com/

No comments:

Post a Comment