连接到 Office 365 时无法显示特定属性

Can't show specific properties when connecting to office 365

所以我正在编写一个脚本来自动连接到 o365 并进行在线交换,我能够做到这一点,然后脚本配偶向我展示了特定的属性,出于某种原因它只选择了一个 属性

$getusrname = read-host "what is the user name?"

Get-Mailbox -Identity *$getusrname* | ForEach-Object { write-host -ForegroundColor White "I found these users: $_"} | select name, @{n="Email adress";e='UserPrincipalName'}

我得到这个输出:

 I found these users: Lev Leiderman

感谢您的帮助

首先是你的顺序错了,因为 write-host 只输出东西到控制台 window 而没有发送任何东西到管道。

其次,您使用的 UserPrincipalName 并不总是 与用户 PrimarySmtpAddress 完全相同(可能是,但并非总是如此)

我认为这会让你更进一步:

$getusrname = Read-Host "what is the user name?"

# instead of using a Filter, you can also experiment with the '-Anr' parameter
# to perform an ambiguous name resolution (ANR) search.
# See: https://docs.microsoft.com/en-us/powershell/module/exchange/mailboxes/get-mailbox?view=exchange-ps#parameters
$users = Get-Mailbox -Filter "Name -like '*$getusrname*'" | 
         Select-Object Name, @{Name = "Email adress"; Expression = 'PrimarySmtpAddress'}
if ($users) { 
    Write-Host "I found these user(s):"
    $users | Format-Table -AutoSize
}
else {
    Write-Host "No user found for $getusrname" -ForegroundColor Red
}