dachiznitt Posted October 8, 2009 Share Posted October 8, 2009 Hi all! Need some help with extracting all email addresses for each object in active directory. The part I am needing help with is getting the info from the mail attribute since it is array. Not sure how to do this and would really appreciate some help with this! Thanks! ;-) Link to comment Share on other sites More sharing options...
99ojo Posted October 9, 2009 Share Posted October 9, 2009 (edited) Hi, download the adfunctions.au3 from "Downloads". #include <adfunctions.au3> $samname = _ADDNToSamAccountName (<DN>) ; e.g. $samname = _ADDNToSamAccountName ("CN=testuser, OU=Test, DC=mydomain,DC=local") ; if you have blanks: ("CN=" & Chr (34) & "test user" & Chr (34) & ", OU=" & Chr (34) & "my ou" & Chr (34) & ", DC=mydomain,DC=local") $mail = _ADGetObjectAttribute ($samname, "mail") $mailother = _ADGetObjectAttribute ($samname, "otherMailbox") MsgBox (0,"",$mail & @CRLF & $mailother) ;-)) Stefan P.S: Edit: Added Attribute otherMailbox Edited October 9, 2009 by 99ojo Link to comment Share on other sites More sharing options...
dachiznitt Posted October 9, 2009 Author Share Posted October 9, 2009 Hi, download the adfunctions.au3 from "Downloads". #include <adfunctions.au3> $samname = _ADDNToSamAccountName (<DN>) ; e.g. $samname = _ADDNToSamAccountName ("CN=testuser, OU=Test, DC=mydomain,DC=local") ; if you have blanks: ("CN=" & Chr (34) & "test user" & Chr (34) & ", OU=" & Chr (34) & "my ou" & Chr (34) & ", DC=mydomain,DC=local") $mail = _ADGetObjectAttribute ($samname, "mail") $mailother = _ADGetObjectAttribute ($samname, "otherMailbox") MsgBox (0,"",$mail & @CRLF & $mailother) ;-)) Stefan P.S: Edit: Added Attribute otherMailbox Thanks, but the notes for that function says it only works with single value attributes. This attribute is an array. Similar to getting a list of group members from a group. Any other ideas? Link to comment Share on other sites More sharing options...
99ojo Posted October 11, 2009 Share Posted October 11, 2009 (edited) Thanks, but the notes for that function says it only works with single value attributes. This attribute is an array. Similar to getting a list of group members from a group.Any other ideas?Hi,so try dsquery with dsget:Query AD for user test user and get sn, givenname and email adress and pipe it to c:\dsget.txt file:dsquery user -name "test user" | dsget user -fn -ln -email >> c:\dsget.txt;-))Stefan Edited October 11, 2009 by 99ojo Link to comment Share on other sites More sharing options...
water Posted October 12, 2009 Share Posted October 12, 2009 Please have a look at this post. It returns an array of all properties for a user, computer or group. You just have to sort out the values you need. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
dachiznitt Posted October 13, 2009 Author Share Posted October 13, 2009 Figured it out.. Code below in case it helps someone. The script creates a tab seperated file with the user DN and all email addresses. One line per user. expandcollapse popup$objConnection = ObjCreate("ADODB.Connection") ; Create COM object to AD $objConnection.ConnectionString = "Provider=ADsDSOObject" $objConnection.Open("Active Directory Provider") ; Open connection to AD $objRootDSE = ObjGet("LDAP://RootDSE") $strDNSDomain = $objRootDSE.Get("defaultNamingContext") ; Retrieve the current AD domain name $strHostServer = $objRootDSE.Get("dnsHostName") ; Retrieve the name of the connected DC $strConfiguration = $objRootDSE.Get("ConfigurationNamingContext") ; Retrieve the Configuration naming context $oMyError = ObjEvent("AutoIt.Error","MyErrFunc") $filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))" $Attributes = "distinguishedName" Local $objRecordSet $objCommand = ObjCreate("ADODB.Command") $objCommand.ActiveConnection = $objConnection $objCommand.Properties("Page Size") = 256 $objCommand.Properties("Searchscope") = 2 $objCommand.Properties("TimeOut") = 20 $strCmdText = "<LDAP://" & $strHostServer & ">;" & $filter & ";" & $Attributes & ";subtree" $objCommand.CommandText = $strCmdText $objRecordSet = $objCommand.Execute $file = FileOpen(@ScriptDir & '\EmailAddresses.txt', 2) If $file = -1 Then MsgBox(0, 'Error - Creating File', 'Unable to create ' & @ScriptDir & '\EmailAddresses.txt') Exit EndIf While Not $objRecordSet.EOF $strdistinguishedName = $objRecordSet.Fields ("distinguishedName").value $Obj = ObjGet('LDAP://' & $strdistinguishedName) If Not @error Then $ObjMail = $Obj.Get ('proxyaddresses') $Emails = '' If Not @error Then For $mail in $ObjMail $Emails = $Emails & $mail & ' ' Next FileWrite ($file, $strdistinguishedName & ' ' & $Emails & @CRLF) EndIf EndIf $objRecordSet.MoveNext WEnd FileClose($file) Func MyErrFunc() $HexNumber = Hex($oMyError.number, 8) If $HexNumber = 80020009 Then SetError(3) Return EndIf Select Case $oMyError.windescription = "Access is denied." $objConnection.Close ("Active Directory Provider") $objConnection.Open ("Active Directory Provider") SetError(2) Case 1 SetError(1) EndSelect EndFunc Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now