Author Posts

November 25, 2013 at 5:14 am

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

November 25, 2013 at 5:22 am

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