0

所以我正在编写一个脚本来自动连接到 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

谢谢你的帮助

4

1 回答 1

0

首先是您的顺序错误,因为write-host仅将内容输出到控制台窗口而没有发送任何内容到管道。

其次,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
}
于 2019-10-31T09:56:46.940 回答