How can i read out each object of a list

Welcome Forums General PowerShell Q&A How can i read out each object of a list

This topic contains 1 reply, has 2 voices, and was last updated by

5 years, 1 month ago.

  • Author
  • #11657

    Points: 0
    Rank: Member

    Hello together

    i have again a question.
    I tried to find out the e-mail with the username of a person and i wrote a script which should read the e-mails out.
    For this i have a list with usernames: @{Extra} @{hofdo} @{Kunfa} @{KIP} and here is the script:
    if($Owner -ne "Extra"){
    $User = Get-WmiObject -Query "SELECT * FROM ds_user where ds_sAMAccountName='$Owner'" -Namespace "root\Directory\LDAP"
    $Email = $user.DS_mail


    I use Windows 7 and Powershell 2.0

    I don't know where the mistake could be
    If you can help please answer me

    sry for my english

  • #11660

    Points: 0
    Rank: Member

    There's not enough context there to see what you're trying to do. You can't start a pipeline with ForEach-Object, though; are you piping something into it? Or did you mean that to be a foreach statement, rather than the ForEach-Object cmdlet?

    Here's what I think your code is trying to do (though I don't know what variable name your list of owners is in, or what type of data is in there. This sample code assumes a simple collection of strings.)

    foreach ($owner in $ownerList)
        if ($owner -ne 'Extra')
            $user = Get-WmiObject -Query "SELECT * FROM ds_user where ds_sAMAccountName='$owner'" -Namespace 'root\Directory\LDAP'
            # In your previous code, you just assigned the user's email address to an $email variable inside the loop, but didn't do anything with it. 
            # Here, I'll just send it as output.

The topic ‘How can i read out each object of a list’ is closed to new replies.