Chapter 11: LDAP Infrastructure Management Code
Discovering the Architecture of an Existing LDAP Directory Using a VBScript Active Server Page
Dim Container Dim Entry Dim StartingPoint 'StartingPoint = "/ou=People" Set Container = GetObject("LDAP://LDAP_SERVER/o=airius.com" & StartingPoint) For Each Entry In Container Response.Write "Entry Name: " & Entry.Name & "Class: " & Entry.Class & "<BR>" Next
Querying Entry Attributes with ADSI Using Anonymous Access in a VBScript Active Server Page
On Error Resume Next Dim Container Dim User Set Container = GetObject("LDAP://LDAP_SERVER/o=airius.com/ou=People") For Each User In Container Response.Write "UserID: " & User.Get("uid") & "Common Name: " & User.Get("cn") & "Surname: " & User.Get("sn") & "<BR>" Next
Querying Entry Attributes with ADSI Using Alternate Credentials in a VBScript Active Server Page
On Error Resume Next Dim dso Dim Container Dim User Set dso = GetObject("LDAP:") Set Container = dso.OpenDSObject("LDAP://LDAP_SERVER/o=airius.com/ou=People", "cn=Directory Manager", "l@undrym@t1974", 0) For Each User In Container Response.Write "UserID: " & User.Get("uid") & "Common Name: " & User.Get("cn") & "Surname: " & User.Get("sn") & "<BR>" Next
Modifying Entry Attributes Using ADSI Using a VBScript Active Server Page
Dim dso Dim Container Dim User Set dso = GetObject("LDAP:") Set Container = dso.OpenDSObject("LDAP://LDAP_SERVER/o=airius.com/ou=People", "cn=Directory Manager", "l@undrym@t1974", 0) Set User = Container.GetObject("inetorgperson", "uid=aknutson") User.Put "sn", "McNally" User.SetInfo
Creating a New Entry Using ADSI In a VBScript Active Server Page
Dim dso Dim Container Dim User Set dso = GetObject("LDAP:") Set Container = dso.OpenDSObject("LDAP://LDAP_SERVER/o=airius.com/ou=People", "cn=Directory Manager", "l@undrym@t1974", 0) Set User = Container.Create("inetorgperson", "uid=teck") User.Put "cn", "Thomas Eck" User.Put "sn", "Eck" User.SetInfo
Removing an Entry Using ADSI In a VBScript Active Server Page
Dim dso Dim Container Set dso = GetObject("LDAP:") Set Container = dso.OpenDSObject("LDAP://LDAP_SERVER/o=airius.com/ou=People", "cn=Directory Manager", "l@undrym@t1974", 0) Call Container.Delete("inetorgperson", "uid=teck")
Using ADO to Search an LDAP Directory Using a VBScript Active Server Page
Dim Connection Dim RS Dim Entry Dim Index Index = 0 Set Connection = New ADODB.Connection Connection.Provider = "ADsDSOObject" Connection.Open "ADSI" Set RS = Connection.Execute("SELECT cn,telephonenumber FROM 'LDAP://LDAP_SERVER/ o=airius.com/ou=people' WHERE sn='carter'") While Not RS.EOF For i = 0 To RS.Fields.Count - 1 If RS.Fields(i).Type = adVariant And Not (IsNull(RS.Fields(i).Value)) Then For j = LBound(RS.Fields(i).Value) To UBound(RS.Fields(i).Value) Entry = Entry & RS.Fields(i).Value(j) & vbTab Next j Else Entry = Entry & RS.Fields(i).Value & vbTab End If If Index = RS.Fields.Count - 1 Then Response.Write Entry & "<BR>" End If Index = Index + 1 Next i Entry = "" Index = 0 RS.MoveNext Wend
Creating a New Exchange Mailbox Using a VBScript Active Server Page
Dim ExchangeServerName Dim ExchangeServerOrganization Dim ExchangeServerSite Dim Mailbox Dim MailboxParentContainer Dim MailboxRelativePath Dim MailboxDisplayName Dim MailboxFirstName Dim MailboxLastName Dim MailboxMiddleInitial Dim MailboxAlias Dim MTA Dim MDB Dim MailboxSMTPAddress Dim MailboxX400Address Dim MailboxCCMailAddress Dim MailboxMSMailAddress Dim NTUserAccountToAssociate Dim UserDomain Dim SID Dim Security Dim SecurityDescriptor Dim DiscretionaryACL Dim AccessControlEntry ExchangeServerName = "EXCHANGE_SERVER" ExchangeServerOrganization = "ADSITest" ExchangeServerSite = "Macmillan" MailboxRelativePath = "" MailboxDisplayName = "Eck, Thomas E." MailboxFirstName = "Thomas" MailboxLastName = "Eck" MailboxMiddleInitial = "E" MailboxAlias = "Thomas.Eck" UserDomain = "UserDomain" NTUserAccountToAssociate = "teck" MTA = "cn=Microsoft MTA,cn=" & ExchangeServerName & ",cn=Servers,cn=Configuration,ou=" & ExchangeServerSite & ",o=" & ExchangeServerOrganization MDB = "cn=Microsoft Private MDB,cn=" & ExchangeServerName & ",cn=Servers,cn=Configuration, ou=" & ExchangeServerSite & ",o=" & ;ExchangeServerOrganization MailboxSMTPAddress = MailboxAlias & "@" & ExchangeServerSite & "." & ExchangeServerOrganization & ".com" MailboxX400Address = "c=US;a= ;p=" & ExchangeServerOrganization & ";o=" & ExchangeServerSite & ";s=" & MailboxLastName & ";g=" & MailboxFirstName & ";i=" & MailboxMiddleInitial MailboxCCMailAddress = MailboxLastName & ", " & MailboxFirstName & " at " & ExchangeServerSite MailboxMSMailAddress = UCase(ExchangeServerOrganization &"/" & ExchangeServerSite & "/" &MailboxAlias) Set MailboxParentContainer = GetObject("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & MailboxRelativePath) Set Mailbox = MailboxParentContainer.Create("organizationalPerson", "cn=" & MailboxAlias) Mailbox.Put "mailPreferenceOption", 0 Mailbox.Put "givenName", MailboxFirstName Mailbox.Put "sn", MailboxLastName Mailbox.Put "cn", MailboxDisplayName Mailbox.Put "uid", MailboxAlias Mailbox.Put "Home-MTA", MTA Mailbox.Put "Home-MDB", MDB Mailbox.Put "mail", MailboxSMTPAddress Mailbox.Put "MAPI-Recipient", True Mailbox.Put "rfc822Mailbox", MailboxSMTPAddress Mailbox.Put "textEncodedORAddress", MailboxX400Address Mailbox.PutEx ADS_PROPERTY_APPEND, "otherMailbox", Array("CCMAIL$" & MailboxCCMailAddress, "MS$" & MailboxMSMailAddress) SID.SetAs ADS_SID_WINNT_PATH, "WinNT://" & UserDomain & "/" & NTUserAccountToAssociate & ",user" sidHex = SID.GetAs(ADS_SID_HEXSTRING) Mailbox.Put "Assoc-NT-Account", sidHex Mailbox.SetInfo Set SecurityDescriptor = Security.GetSecurityDescriptor(Mailbox.ADsPath) Set DiscretionaryACL = SecurityDescriptor.DiscretionaryACL AccessControlEntry.AceType = ADS_ACETYPE_ACCESS_ALLOWED AccessControlEntry.Trustee = UserDomain & "\" & MailboxAlias AccessControlEntry.AccessMask = ADS_RIGHT_EXCH_MAIL_SEND_AS Or ADS_RIGHT_EXCH_MAIL_RECEIVE_AS Or ADS_RIGHT_EXCH_MODIFY_USER_ATT DiscretionaryACL.AddAce AccessControlEntry SecurityDescriptor.DiscretionaryACL = DiscretionaryACL Security.SetSecurityDescriptor SecurityDescriptor
Removing an Existing Exchange Mailbox Using a VBScript Active Server Page
Dim ExchangeServerName Dim ExchangeServerOrganization Dim ExchangeServerSite Dim MailboxParentContainer Dim MailboxRelativePath Dim MailboxToRemove ExchangeServerName = "EXCHANGE_SERVER" ExchangeServerOrganization = "ADSITest" ExchangeServerSite = "Macmillan" MailboxRelativePath = "" MailboxToRemove = "Thomas.Eck" Set MailboxParentContainer = GetObject("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & MailboxRelativePath) Call MailboxParentContainer.Delete("organizationalPerson", "cn=" & MailboxToRemove)
Adding a Distribution List Member Using a VBScript Active Server Page
Dim ExchangeServerName Dim ExchangeServerOrganization Dim ExchangeServerSite Dim DistributionList Dim DistributionListName Dim DistributionListRelativePath Dim DLMemberMailboxName ExchangeServerName = "EXCHANGE_SERVER" ExchangeServerOrganization = "ADSITest" ExchangeServerSite = "Macmillan" DistributionListRelativePath = "" DistributionListName = "DL_Research_Development" DLMemberMailboxName = "Thomas.Eck" Set DistributionList = GetObject("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & DistributionListRelativePath & "/cn=" & DistributionListName) DistributionList.Add ("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & "/cn=" & DLMemberMailboxName) DistributionList.SetInfo
Removing a Distribution List Member Using a VBScript Active Server Page
Dim ExchangeServerName Dim ExchangeServerOrganization Dim ExchangeServerSite Dim DistributionList Dim DistributionListName Dim DistributionListRelativePath Dim DLMemberMailboxName ExchangeServerName = "EXCHANGE_SERVER" ExchangeServerOrganization = "ADSITest" ExchangeServerSite = "Macmillan" DistributionListRelativePath = "" DistributionListName = "DL_Engineering" DLMemberMailboxName = "Thomas.Eck" Set DistributionList = GetObject("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & DistributionListRelativePath & "/cn=" & DistributionListName) DistributionList.Remove ("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & "/cn=" & DLMemberMailboxName) DistributionList.SetInfo
Determining the Distribution List Owner Using a VBScript Active Server Page
Dim ExchangeServerName Dim ExchangeServerOrganization Dim ExchangeServerSite Dim DistributionList Dim DistributionListName Dim DistributionListRelativePath ExchangeServerName = "EXCHANGE_SERVER" ExchangeServerOrganization = "ADSITest" ExchangeServerSite = "Macmillan" DistributionListRelativePath = "" DistributionListName = "DL_Senior_Management" Set DistributionList = GetObject("LDAP://" & ExchangeServerName & "/o=" & ExchangeServerOrganization & "/ou=" & ExchangeServerSite & "/cn=Recipients" & DistributionListRelativePath &"/cn=" & DistributionListName) Response.Write DistributionList.Owner