Sign in to follow this  
Followers 0
danchitu86

How to get e-mail address from AD?

3 posts in this topic

Hello everybody!

I need to make a script to query the Active Directory for the E-mail address for loged-on user.

If the e-mail address field has a value in AD, the script work's ok, returns that value (see testemailok.PNG).But if this field is blank I got this error:

$mailad = $oObject.Get ("mail")

$mailad = $oObject.Get ("mail")^ ERROR

->15:11:53 AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 2.114

When running the .exe file for this script I got the error attached (see error.png).

How can I set AutoIt to handle that blank field without error? I need to use $mailad value in the same script for other actions.

Thanks a lot!

My code is:

#include <Array.au3>
Global $objConnection = ObjCreate("ADODB.Connection")  ; Create COM object to AD
$objConnection.ConnectionString = "Provider=ADsDSOObject"
$objConnection.Open ("Active Directory Provider")  ; Open connection to AD
Global $objRootDSE = ObjGet("LDAP://RootDSE")
Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext")  ; Retrieve the current AD domain name
Global $strHostServer = $objRootDSE.Get ("dnsHostName") ; Retrieve the name of the connected DC
Global $strConfiguration = $objRootDSE.Get ("ConfigurationNamingContext") ; Retrieve the Configuration naming context
$object=@UserName
$strQuery = "<LDAP://" & $strHostServer & "/" & $strDNSDomain & ">;(sAMAccountName=" & $object & ");ADsPath;subtree"
                $objRecordSet = $ObjConnection.Execute ($strQuery)  ; Retrieve the FQDN for the object
                $ldap_entry = $objRecordSet.fields (0).value
                $oObject = ObjGet($ldap_entry)  ; Retrieve the COM Object for the object
                $oObject.GetInfo
                $mailad = $oObject.Get ("mail")
                $oObject.PurgePropertyList
                $oObject = 0
    msgbox (4096, "Mail", $mailad)

post-68105-0-07926700-1320066997_thumb.p

post-68105-0-61623200-1320067004_thumb.p

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You could use my Active Directory UDF (for download please see my signature). The UDF handles errors automatically and sets @error.

To get the property "mail" for the current user you simply need to run something like:

#include <AD.au3>
_AD_Open()
$sResult = _AD_GetObjectAttribute(@username, "mail")
If @error = 2 Then MsgBox(16, "Error", "Attribute 'mail' does not exist for user " & @username)
_AD_Close()
Edited by water

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

Thanks a lot! It works!

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  
Followers 0