Get-Emails from AD

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Brad Brad 5 months, 1 week ago.

  • Author
    Posts
  • #64942
    Profile photo of Brad
    Brad
    Participant

    Hello everyone,

    need your help,
    i have a list with emails(some of this users still in ad some left)
    i need find information based on this emails, i need the email associated to user who not in AD still stay in report with error message
    my script

    $csv = get-content "c:\Email.csv"
    $results = foreach($mail in $csv){
        $userMail = $user.mail
        Get-ADUser -Filter {mail -eq $mail} -Properties Name, mail,Description,Title,Manager | Select-Object Name, mail,Description,Title,Manager 
    
    }$results | Export-Csv "c:\Email_Report.csv" -NoTypeInformation
    

    this part working ok, but it removing from report email which not exist in AD, but i need them in report

    Thanks.

  • #64948
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There are a lot of ways to do things. First, your loop was incorrect. You were using foreach ($mail in $csv) and referencing $userMail as $user.Mail and then your filter as $mail, which would be an object unless you did $mail.Mail.

    The updated code will create an object if the user was found or not found in AD. Status was added to quickly identify if finding the user was Successful or Failed, but you could even just use Name to see if is $null to see which failed.

    $csv = get-content "c:\Email.csv"
    
    $results = foreach($user in $csv){
        $userMail = $user.mail
        $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, mail,Description,Title,Manager
                   
        if ($adUser) {
            $adUser |
            Select-Object Name, 
                          mail,
                          Description,
                          Title,
                          Manager,
                          @{Status="Status";Expression={"Success"}}
        }
        else {
            New-Object -TypeName PSObject -Property @{
                Name        = $null
                mail        = $userMail
                Description = $null
                Title       = $null
                Manager     = $null
                Status      = "Failed"
            }
        }
    }
    
    $results | Export-Csv "c:\Email_Report.csv" -NoTypeInformation
    
  • #64951
    Profile photo of Brad
    Brad
    Participant

    Hi Rob,

    Thank you for example, but i get the error:

    Get-ADUser : Variable: 'userMail' found in expression: $userMail is not defined.
    At line:5 char:15
    + ...   $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : Variable: 'userMail' found in expression: $userMail is not defined.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
     
    Select-Object : The Status key is not valid.
    At line:9 char:9
    
  • #64956
    Profile photo of Ron
    Ron
    Participant

    From your first script, I assume that this file is not actually a csv, just a text file with a list of email addresses.

    If so remove this line.

    $userMail = $user.mail

    Change this line.

    $results = foreach($userMail in $csv){

  • #64959
    Profile photo of Brad
    Brad
    Participant

    Hi Ron,

    Thank you again,but still error:

    Select-Object : The Status key is not valid.
    At line:10 char:9
    +         Select-Object Name,
    +         ~~~~~~~~~~~~~~~~~~~

    and i changed script

    $results = foreach($userMail in $csv){
        #$userMail = $user.mail
        $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, mail,Description,Title,Manager
    
  • #64963
    Profile photo of Ron
    Ron
    Participant

    Try this.

    @{ Name = "Status"; Expression = { "Success" } }

    It doesn't like Status="Status"

  • #64968
    Profile photo of Brad
    Brad
    Participant

    Hi Ron,

    Thank you
    its still give me error:

    Get-ADUser : The search filter cannot be recognized
    At line:6 char:15
    + ...   $adUser = Get-ADUser -Filter {mail -eq $userMail} -Properties Name, ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    but this time it create report
    Thanks.

  • #64972
    Profile photo of Ron
    Ron
    Participant

    Can you show me a sample of the file? Be sure obfuscate any sensitive info.

  • #64975
    Profile photo of Brad
    Brad
    Participant

    Ron – Thank you again
    i find my problem
    i try to read data from csv file, i change it on txt and all work

    Thanks again.

You must be logged in to reply to this topic.