【问题标题】:Can't show specific properties when connecting to office 365连接到 Office 365 时无法显示特定属性
【发布时间】:2019-10-31 07:06:09
【问题描述】:

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

感谢您的帮助

【问题讨论】:

    标签: powershell office365 select-object


    【解决方案1】:

    第一件事是您的顺序错误,因为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
    }
    

    【讨论】:

    • 非常感谢您的帮助,它确实有效,但是对于这种“糟糕的任务”来说行数太多了,您能告诉我“-filter”和“-identity”有什么区别吗?跨度>
    • @WebsGhost 行数过多?尝试在单行中猛烈抨击所有内容是一件坏事,因为正如您所注意到的,使用这些很容易出错并且很难检测到它们。 -Filter 可以使用像* 这样的通配符。对于-Identity,您需要准确指定邮箱。见Get-Mailbox
    • 我不得不承认,有一点……我试过-anr,但它没有用。
    • @WebsGhost 对于-Anr,您不能使用通配符* 字符。你看过我链接的官方文档吗?
    • 是的,我的朋友我有一些拼写错误:) 谢谢!
    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 2012-04-27
    相关资源
    最近更新 更多