获取当前的Exchange Server

时间:2016-08-03 14:12:40

标签: powershell exchange-server

我正在使用EMS。调用 Get-ExchangeServer 命令时,我获得了可用的Exchange服务器列表。

现在的问题是:

如何获取当前的Exchange服务器(我调用 Get-ExchangeServer 命令的服务器)?

任何人都可以建议如何实现它吗?

2 个答案:

答案 0 :(得分:1)

使用环境变量$ENV:COMPUTERNAME检索要在其上调用命令的Exchange服务器的服务器名称。

Invoke-Command -ComputerName Exchange.domain.com `
               -ScriptBlock {Get-ExchangeServer -Identity $ENV:COMPUTERNAME} `
               -Credential (Get-Credential)

答案 1 :(得分:0)

  • 简答:

    Get-ExchangeServer | select PSComputerName,OriginatingServer -Unique

  • 解释(长答案):

    命令Get-ExchangeServer的输出,已经有你感兴趣的成员:

PS:\ > Get-ExchangeServer | Get-Member

Name                    MemberType     (my explanation)
----                    ----------     -----------------
.                       .              .
PSComputerName          NoteProperty   It's the current ExchangeServer (the one that actually your command has been executed on)
OriginatingServer       Property       It's the current DomainController (the one that your ExchangeServer has used for the query)
.                       .              .

然后,使用EMS或PSRemoting,它总是返回您想要的。但是,如果您的环境中有多个 ExchangeServer,则输出可能包含具有相同值的多行。喜欢:

PS:\ > Get-ExchangeServer | select PSComputerName,OriginatingServer

PSComputerName OriginatingServer      
-------------- -----------------      
exch-srv2      dc3.contoso.com
exch-srv2      dc3.contoso.com
.              .

这就是为什么您可以在执行 -unique 时使用 Select 以避免输出中出现重复的行。

PS:\ > Get-ExchangeServer | select PSComputerName,OriginatingServer -Unique

PSComputerName OriginatingServer      
-------------- -----------------      
exch-srv2      dc3.contoso.com