Jump to content

How to get e-mail address from AD?


Recommended Posts

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

Link to comment
Share on other sites

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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...