0

I am trying to get the SamAccountName from Get-ADUser, but when I pass in the variable $member.User I get no results. $member.User when I print it out with Write-Host returns a variable, but used in the code below I get nothing. Also, if I copy/paste the $member.User value into that $add = Get-ADUser I get the SamAccountName. Why isn't Get-ADUser -Filter {EmailAddress -eq "$member.User" } returning anything? It is driving me nuts. Thank you in advance.

$Send = @()
$SendAs = @()
# Displaying FullAccess permissions for shared mailboxes
$Send = Get-MailboxPermission -Identity $MailboxUPN | Where-Object { -not ($_.User -like “NT AUTHORITY\SELF”) } | Select-Object Identity,User,AccessRights
$Send
# Displaying SendAs permissions for shared mailboxes
# $SendAs = Get-RecipientPermission -Identity $MailboxUPN | Where-Object {($_.IsInherited -eq $False) -and -not ($_.Trustee -like “NT AUTHORITY\SELF”) } | Select-Object Trustee, AccessRights
# $SendAs

forEach ($member in $Send){
   Write-Host "In here"
   Write-Host $member
   $add = Get-ADUser -Filter {EmailAddress -eq "$member.User" }| Select-Object -ExpandProperty SamAccountName
   Write-Host $add.SamAccountName
}
  • This issue has been discussed several times already. Please see. https://stackoverflow.com/a/44184818/13699968 – swbbl Feb 14 '21 at 06:31
  • this >>> `"$member.User"` <<< will not be expanded. instead, you will get the `.ToString()` of `$Member` plus the literal `.User`. you need to force the property to be evaluated by using >>> `"$($member.User)"` << – Lee_Dailey Feb 14 '21 at 06:42
  • @Lee_Dailey Yes, Subexpressions are generally mandatory in PowerShell when a member access (e.g. Property) takes place. But it will not work in the `Filter` parameter of `Get-AdUser`, especially since the Parameter takes a String, not a ScriptBlock. – swbbl Feb 14 '21 at 06:54
  • 2
    @pwnosh - thank you for that info. [*grin*] so i suppose the proper way would be >>> `-Filter "EmailAddress -eq '$($member.User)'"` << – Lee_Dailey Feb 14 '21 at 07:13
  • Thank you @pwnosh. I will check that out now. – someplaceinthemall Feb 14 '21 at 17:52
  • @Lee_Dailey that still didn't work for me. I got no value returned. – someplaceinthemall Feb 14 '21 at 17:52
  • @pwnosh my answer was in there. Thank you so much for that link. That was very helpful. – someplaceinthemall Feb 14 '21 at 18:25

0 Answers0