List Exchange Servers from a non-exchange computer
# Obtain a list of all on premise Exchange servers
$searchBase=(Get-ADOrganizationalUnit -Server (get-addomaincontroller).hostname -Filter 'Name -like "SERVERS"').DistinguishedName
Get-ADComputer -Filter * -SearchBase $searchBase -Properties * | Where-Object {$_.serviceprincipalname -like '*Exchange*'} |select-object Name
Output
PS C:\Windows\system32> Get-ADComputer -Filter * -SearchBase $searchBase -Properties * | Where-Object {$_.serviceprincipalname -like '*Exchange*'} |select-object Name
Name
----
EXCH01
EXCH02
# Alternative
Get-ADGroup -Identity "Exchange Servers" | Get-ADGroupMember | Format-Table -AutoSize
Output:
PS C:\Windows\system32> Get-ADGroup -Identity "Exchange Servers" |Get-ADGroupMember | Format-Table -AutoSize
distinguishedName name
----------------- ----
CN=EXCH02,OU=Exchange,OU=Servers,DC=corp,DC=kimconnect,DC=com EXCH02
CN=EXCH01,OU=Exchange,OU=Servers,DC=corp,DC=kimconnect,DC=com EXCH01
CN=Exchange Install Domain Servers,CN=Microsoft Exchange System Objects,DC=corp,DC=kimconnect,DC=com Exchange Install Domain Servers group
# Enabling WinRM on Exchange Server
$searchBase=(Get-ADOrganizationalUnit -Filter 'Name -like "SERVERS"').DistinguishedName
$exchangeServer1 = (Get-ADComputer -Filter * -SearchBase $searchBase -Properties * | Where-Object {$_.serviceprincipalname -like '*Exchange*'} | select-object Name).Name[0]
function pingTest{
Param([string]$node)
try{
Return Test-Connection $node -Count 1 -Quiet -ea Stop;
}
catch{Return $False}
}
function enableRemoteWinRM{
Param([string]$computername)
if (pingTest $computername){
try{
$isWinRMEnabled = Test-WSMan $computername -ea Stop
}
catch{
$isWinRMEnabled=$False
Write-Host "WinRM has not been detected. Enabling now..."
continue;
}
if (!($isWinRMEnabled)){psexec.exe \\$computername -s C:\Windows\system32\winrm.cmd qc -quiet}
else{Write-Host "WinRM has been already enabled. No changes to WinRM have been made."}
}
Else{Write-Host "Unable to determine if WinRM is enabled on $computername`.`n Ping test has failed. Check if this computer is online and whether there's a firewall blocking of ICMP";}
}
enableRemoteWinRM -ComputerName $exchangeServer1
# Obtain Exchange Server Roles
$searchBase=(Get-ADOrganizationalUnit -Filter 'Name -like "SERVERS"').DistinguishedName
$exchangeServer1 = (Get-ADComputer -Filter * -SearchBase $searchBase -Properties * | Where-Object {$_.serviceprincipalname -like '*Exchange*'} | select-object Name).Name[0]
$cred = Get-Credential
<# WinRM session doesn't work because it's being run as unelevated admin
Invoke-Command -computername $exchangeServer1 -scriptblock {
#Requires -RunAsAdministrator
$isAdmin=([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
Write-Host "Running as Administrator: $isAdmin"
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Snapin;
Get-ExchangeServer | select name, serverrole, edition, admindisplayversion, isClientAccessServer | fl;
} -credential $cred
# Implicit PowerShell Session Importing as a method to connect to Exchange server as Elevated session
function connectToExchange{
$Cred = (Get-Credential)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$exchangeServer1`.$((get-addomain).DNSRoot)/powershell" -Credential $Cred
Import-PSSession $Session
}
#>
function discoverExchangeServers{
connectToExchange;
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Snapin;
Get-ExchangeServer | select name, serverrole, edition, admindisplayversion, isClientAccessServer | fl;
}
Output:
PS C:\Windows\system32> discoverExchangeServers
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At line:2 char:1
+ Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Snapin;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Excha...werShell.Snapin:String) [Add-PSSna
pin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
Name : EXCH02
ServerRole : Mailbox
Edition : Enterprise
AdminDisplayVersion : Version 15.1 (Build 466.34)
IsClientAccessServer : True
Name : EXCH01
ServerRole : Mailbox
Edition : Enterprise
AdminDisplayVersion : Version 15.1 (Build 466.34)
IsClientAccessServer : True
Categories: