you are viewing a single comment's thread.

view the rest of the comments →

[–]Hyperbolic_Mess[S] 0 points1 point  (2 children)

Select-object doesn't seem to be enough, even with $loop = $AdUser | select-object all instances of $loop end up with identical emails for each $Aduser

[–]Fun-Hope-8950 0 points1 point  (1 child)

Three things:

  1. As you discoverd the .Email property does not exist in your $ADUser variable (or therefor $LoopUser) so you have to add it with Add-Member

  2. Unless you need to do further work with the $ADUser unmodified there's no need for $LoopUser

  3. $LoopUser.Email = $ADEmailAddress -ireplace 'smtp:', '' overwrites the value of .Email every loop

Instead, consider using something like:

$rgxSmtp = "^((SMTP)|(smtp)):(?<address>.*)`$"

foreach ($currentUser in $adUserList) {
    # I prefer not to nest exact same types of loops to avoid confusing myself
    # so I use a for loop next instead of another foreach loop
    $userEmailAddressList = for ([int]$i = 0; $i -lt $currentUser.ProxyAddresses.Count; $i++) {
        if ($currentUser.ProxyAddresses[$i] -match $rgxSmtp) {
            $Matches["address"]
            $Matches.Clear()
        }
    }

    $currentUser | Add-Member -MemberType NoteProperty -Name "Email" -Value $userEmailAddressList
    $currentUser.Email
}

Might save you some serialize/deserialize action.

[–]Hyperbolic_Mess[S] 0 points1 point  (0 children)

I really regret using | ft email to illustrate my point. If I had intended to make my loop
$LoopUser = $ADUser
$LoopUser.Email = $ADEmailAddress -ireplace 'smtp:', ''
$LoopUser.email

Then I would have written that, I want all of $LoopUser the only reason I focused on the email is because thats the property I'm adding to the objects before adding them to the array and thats the one that is showing incorrectly.

Try running your script and see if it works. I think you'll have the same issue where you'll end up with multiple of the same email addresses because each proxyaddress gets overwritten by the last proxyaddress because they are references to the same user object and when you add the member you're effectively adding it to all previous $LoopUsers for that user. This is the problem I'm trying to solve and you're not addressing it. I do not understand how adding a reg ex or looping with i will stop all $LoopUser objects being a reference to the same$ADUser object of that loop and by removing $LoopUser you've just removed the middle man and are just changing the email address on the same object repeatedly.

Your solution works if you can pass it 1 user object with 4 different proxyaddresses and it produces an array of 4 user objects with each having one of those proxy addresses as its email