Jump to content

Recommended Posts

Posted

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!

;-)

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

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?

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

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

 

Posted

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

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
  • Recently Browsing   0 members

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