How can i read out each object of a list

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 5 months ago.

  • Author
    Posts
  • #11657
    Profile photo of Dominique Hofmann
    Dominique Hofmann
    Participant

    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:
    foreach-object{
    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.
    
            $user.DS_mail
        }
    }
    

You must be logged in to reply to this topic.