0

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

Tomer Schweid
  • 325
  • 2
  • 5
  • 18
  • 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 Feb 20 '17 at 12:29
  • It doesn't really matter. If I'll get a list I'll just try the first one – Tomer Schweid Feb 20 '17 at 12:37

2 Answers2

1

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
Matt
  • 45,022
  • 8
  • 78
  • 119
0

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 = "your.email@domain.com"

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

# discover the url from your email address
$s.AutodiscoverUrl($email)
$s.Url.AbsoluteUri
Community
  • 1
  • 1
TechSpud
  • 3,418
  • 1
  • 27
  • 35
  • First of all, thank you for your reply. I am in a closed network so transferring installations is a bit of an issue. I'll try that if there will be no other option – Tomer Schweid Feb 20 '17 at 12:04
  • 1
    Calling this method should return the Autodiscover endpoint : this is in multi server exchange environments usually not the mailbox server itself (in a lot of cases this is the CAS) – bluuf Feb 20 '17 at 12:19
  • Oh, well. As I said, I found a way to discover the CAS, and yes - the mailbox is not on the same server – Tomer Schweid Feb 20 '17 at 12:36
  • @TomerSchweid You could now connect to a session on the CAS server and query all the mailbox servers? This is one of the things I would have suggested given the constraints – Matt Feb 20 '17 at 12:39
  • I don't know how to do such thing. If you meant through powershell then it can't be done since it needs to connect to a mailbox server. If you have another way - please share – Tomer Schweid Feb 20 '17 at 12:42
  • You can connect to a CAS session via PowerShell. It does not have to be a mailbox server. I normally connect to exchange via CAS anyway. Once connected you can run exchange `Get-MailboxServer`. This question is about finding the server not connecting to it. – Matt Feb 20 '17 at 12:46