Reputation: 67
I'm using EMS. When invoking Get-ExchangeServer command I'm getting list of available Exchange servers.
Now the question:
How to get current Exchange server (the one I've invoked Get-ExchangeServer command on)?
Can anyone advice how to achieve it?
Upvotes: 3
Views: 1541
Reputation: 61
Short Answer:
Get-ExchangeServer | select PSComputerName,OriginatingServer -Unique
Explanation (Long Answer):
The output of the command Get-ExchangeServer
, already have the members that you are interested in :
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)
. . .
Then, using EMS or PSRemoting, It always returns what you want. however, the output may include multiple rows with the same value if you have more than one ExchangeServer in your environment. like :
PS:\ > Get-ExchangeServer | select PSComputerName,OriginatingServer
PSComputerName OriginatingServer
-------------- -----------------
exch-srv2 dc3.contoso.com
exch-srv2 dc3.contoso.com
. .
that's why you can use -unique
while doing Select
, to avoid duplicate rows within the output.
PS:\ > Get-ExchangeServer | select PSComputerName,OriginatingServer -Unique
PSComputerName OriginatingServer
-------------- -----------------
exch-srv2 dc3.contoso.com
Upvotes: 0
Reputation: 7000
Use the environment variable $ENV:COMPUTERNAME
to retrieve the server name of the exchange server that you are invoking the command on.
Invoke-Command -ComputerName Exchange.domain.com `
-ScriptBlock {Get-ExchangeServer -Identity $ENV:COMPUTERNAME} `
-Credential (Get-Credential)
Upvotes: 1