none
For Each Help RRS feed

  • Question

  • I am struggling with what I suspect is a lack of basic understanding of using for each to run a script block against a list.  I have read numerous articles detailing how simple it is.  Thank you in advance if you can point out what I am missing.  I am trying to take a CSV of display names and retrieve their primary email address.  

    I tried this but it returns all users email addresses instead of just my list.

    $a = Get-Content "C:\users\xxxxx\desktop\DN.csv"
    foreach ($i in $a) {
        Get-ADUser -Filter * -Properties * | select mail
        }

    I also tried this but I got the same result.  I keep getting results outside my list.

    foreach ($i in Get-Content "C:\users\xxxxx\desktop\DN.csv")
    {Get-ADUser -Filter * -Properties * | select mail}

    If anyone can point out what I am missing I would really appreciate it.  Thank you.


    ~Eric

    Tuesday, May 23, 2017 6:40 PM

Answers

  • Correction - instead of $_.mail, it should be $user.mail.


    Get-Content DNs.txt | ForEach-Object {
      $user = Get-ADUser -LDAPFilter "(displayName=$_)" -Properties mail
      if ( $user ) {
        [PSCustomObject] @{
          displayName = $_
          mail        = $user.mail
        }
      }
    }

    Note that this requires at least PowerShell v3 to run properly.

    -- Bill Stewart [Bill_Stewart]


    Tuesday, May 23, 2017 9:14 PM
    Moderator

All replies

  • If the file really is a CSV, you should use Import-Csv rather than Get-Content.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, May 23, 2017 7:04 PM
    Moderator
  • Not to mention the lack of an example of what is in the CSV file??????

    help import-csv -full


    \_(ツ)_/


    • Edited by jrv Tuesday, May 23, 2017 8:12 PM
    Tuesday, May 23, 2017 8:11 PM
  • The CSV file has user display names, one in each line:

    They are in the format of lastname, firstname (City) i.e. Smith, John (Los Angeles)


    ~Eric

    Tuesday, May 23, 2017 8:24 PM
  • So then it is not a CSV file at all, then, but rather just a plain text file with one display name per line. Is that correct? If so, then do not name the file with a .csv extension, to avoid confusion. (CSV means "comma-separated values.")


    Get-Content DNs.txt | ForEach-Object {
      $user = Get-ADUser -LDAPFilter "(displayName=$_)" -Properties mail
      if ( $user ) {
        [PSCustomObject] @{
          displayName = $_
          mail        = $_.mail
        }
      }
    }
    


    -- Bill Stewart [Bill_Stewart]


    Tuesday, May 23, 2017 8:43 PM
    Moderator
  • HI Eric, 

    not sure how your file looks like, but your foreach-Blocks do not have a reference to the contents.

    Let's assume you have a file "names.csv" with this content

    id;firstname;lastname;logonname
    1;MARY;Smith;REAGMARY1
    2;PATRICIA;Williams;MCCLPATR2
    3;LINDA;Jones;HOUGLIND3

    then you can do it like that

    #import csv
    $filecontent = import-csv -Delimiter ';' names.csv
    
    $filecontent | foreach-object {
        # $_ contains the current line content
         $line = $_
         write-host "processing " $line.logonname
         $userobject = get-aduser $line.logonname
         # add some stuff to process $userobject
    }

    $line.logonname in my script refers to the column $logonname defined above in the csv. 

    In your snipplet the reference to the object you want to modify seems to be missing.
    You never used $_ in your foreach-object script block nor $i in your foreach loop, so you're not working on the current line.

    Let me know if it helped.

    Best regards,
    Martin


    Tuesday, May 23, 2017 8:46 PM
  • Originally, the names were is an excel spreadsheet.  I took the names and pasted them into a new spreadsheet, which I saved with the .csv extension.

    I renamed to text and ran your suggestion.  The output is showing the display names from the text file but returning blanks for mail.  I think mail is definitely the correct property.


    Name                           Value                                                                                                                                                                                                                              
    ----                           -----                                                                                                                                                                                                                               
    displayName                    XXXX, Michael (XXX - Carolinas)                                                                                                                                                                                           
    mail                                                                                                                                                                                                                                                               
    displayName                    XXXXX Ken (XXX - Carolinas)                                                                                                                                                                                                    
    mail                                                                                                                                                                                                                                                               
    displayName                   XXXX, Jenna (XXX - Chicago)                                                                                                                                                                                                       
    mail                                                                                                                                                                                                                                                               
    displayName                    XXXX, Erica (XXX - Cincinnati)


    ~Eric

    Tuesday, May 23, 2017 9:02 PM
  • Correction - instead of $_.mail, it should be $user.mail.


    Get-Content DNs.txt | ForEach-Object {
      $user = Get-ADUser -LDAPFilter "(displayName=$_)" -Properties mail
      if ( $user ) {
        [PSCustomObject] @{
          displayName = $_
          mail        = $user.mail
        }
      }
    }

    Note that this requires at least PowerShell v3 to run properly.

    -- Bill Stewart [Bill_Stewart]


    Tuesday, May 23, 2017 9:14 PM
    Moderator
  • That works thank you.  

    ~Eric

    Tuesday, May 23, 2017 9:32 PM
  • Thank you very helpful explanation.

    ~Eric

    Tuesday, May 23, 2017 9:33 PM