Jakub Krupa Jakub Krupa - 4 months ago 18
PowerShell Question

Get current Exchange Server

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?

Answer

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)