0
votes

I'm trying to write down a powershell script that will automatically finds the mailbox server and connect to it (using the URI https://mailboxserver/powershell).

Problem is I haven't found a way to automatically detect a mailbox server for a given exchange organization. I found a way how to find the CAS server because someone posted how the outlook finds this manually.

I tried to query AD but I do not know which attribute is unique to exchange mailbox server.

I also tried DNS records but found none which helps.

Does anybody know about a unique value of mailbox server which could be queried from AD or GC? Or a DNS record or something else I have not thought of?

Exchange 2010 I could post forest and domain functional level if necessary but I am on the way.

Thanks in advance

2
How do you decide which mailbox server to connect to when there are multiples? Does having previous knowledge of a CAS violate what you are trying to do?Matt
It doesn't really matter. If I'll get a list I'll just try the first oneTomer Schweid

2 Answers

1
votes

Your AD user attributes have this information, albeit you have to parse the mailbox server name from them.

  • HomeMTA
  • msExchHomeServerName

So if you have access to the AD cmdlets you might be able to get your mailbox server this way.

$adUser = get-aduser someuser -Properties msExchHomeServerName
$mailboxServerName = ($aduser.msExchHomeServerName -split "cn=")[-1]

Those attributes help you find your current mailbox is hosted. The mailbox server in my case was the last "item" in msExchHomeServerName so I split the string on "cn=" and then the last element of that array would be my mailbox server name.

Then you can use that to connect to an Exchange session!

$Credentials = Get-Credential
$exchangePath = "http://$mailboxServerName/PowerShell/?SerializationLevel=Full"
$ExSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri $exchangePath  -Credential $Credentials –Authentication Kerberos
Import-PSSession $ExSession
0
votes

Does the code below get you what you need? It uses EWS - see my SO post for further details on EWS.

# load the assembly
[void][Reflection.Assembly]::LoadFile("D:\Temp\Microsoft.Exchange.WebServices.2.2\lib\40\Microsoft.Exchange.WebServices.dll")

# set ref to exchange - may need to change the version
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)

# replace with your email address
$email = "[email protected]"

# grab your own credentials
$s.UseDefaultCredentials = $true

# discover the url from your email address
$s.AutodiscoverUrl($email)
$s.Url.AbsoluteUri