Jump to content
Sign in to follow this  
dachiznitt

Active Directory mail Attribute

Recommended Posts

dachiznitt

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!

;-)

Share this post


Link to post
Share on other sites
99ojo

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 by 99ojo

Share this post


Link to post
Share on other sites
dachiznitt

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?

Share this post


Link to post
Share on other sites
99ojo

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 by 99ojo

Share this post


Link to post
Share on other sites
water

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
dachiznitt

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.

$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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.