Wednesday, September 13, 2006

Showing the Path to a public folder based on the Email address

This was another one from the mailbag this week somebody asked if it was possible to display the path to a public folder based on the SMTP address. There are a few approaches you could take for this one method is to use ADSI and WMI to do this. On exchange 2003 the Exchange_PublicFolder WMI class can be used to display a lot of information about public folders on an Exchange server (such as folder path etc). But one thing that isn’t stored is the smtp email address assigned to that folder these are stored on the AD object for that folder stored in the Microsoft Exchange System Objects container. So if you want to find the path to a folder using the WMI Exchange_Public folder class from a SMTP address you first want to use ADSI to query the Proxyaddresses AD attribute to find the AD object for that public folder. Then you can use one of two attributes to find the folder using a WMI query the first attribute you could use is the legacyExchangeDN which should correspond to the TargetAddress property in WMI. The other property you could use is objectguid which after you transpose it correctly should match the adproxypath in WMI. The one I chose to use was the ADproxypath (for reasons that may or may not become clear later).

To run this script it takes 2 command line parameters the first is an Exchange servername which will be used to make the WMI query.(this should be an Exchange server where there is an instance of the public folder you are looking for). And the second is the SMTP address of the folder you are looking for. Eg cscript showfoldv1.vbs servername emailaddress@domain.com

I put a downloadable copy of the code here the script itself looks like


Email = "smtp:" & wscript.arguments(1)
ExchangeServer = wscript.arguments(0)
Set rootDSE = GetObject("LDAP://RootDSE")
domainContainer = rootDSE.Get("defaultNamingContext")
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"
LDAPStr = "<LDAP://" & DomainContainer & ">;(&(objectCategory=publicfolder)(proxyAddresses="
& email & "));adspath,objectguid;subtree"
Set rs = conn.Execute(LDAPStr)
If rs.RecordCount = 1 Then
wscript.echo
FindPublicFolderWMI(transposeGuid(ConvertObjectGuidToString(rs.fields("objectguid"))))
End If

Function ConvertObjectGuidToString(ByVal arrRawObjectGUID)
Dim i, strByte
Dim arrObjectGUID(15)
For i = 1 To LenB(arrRawObjectGUID)
strByte = Hex(AscB(MidB(arrRawObjectGUID, i, 1)))
If Len(strByte) = 1 Then strByte = "0" & strByte
arrObjectGUID(i - 1) = strByte
Next
ConvertObjectGuidToString = Join(arrObjectGUID, "")
End Function

Function transposeGuid(guid)
transposeGuid = "{" & mid(guid,7,2) & mid(guid,5,2) & mid(guid,3,2) _
& mid(guid,1,2) & "-" & mid(guid,11,2) & mid(guid,9,2) _
& "-" & mid(guid,15,2) & mid(guid,13,2) & "-" & mid(guid,17,4) _
& "-" & mid(guid,21,12) & "}"
end function

Function FindPublicFolderWMI(AdproxyPath)

Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_PublicFolder"
strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
ExchangeServer &"/"&cWMINameSpace
Set objWMIServices = GetObject(strWinMgmts)
Set objPubInstances = objWMIServices.ExecQuery ("Select * From
Exchange_PublicFolder Where adproxyPath='" & AdproxyPath & "'")
For Each objExchange_PublicFolder in objPubInstances
path = objExchange_PublicFolder.Path
Next
FindPublicFolderWMI = path
End function

12 comments:

Anonymous said...

Looks like an interesting piece of code, but I could not get it to work.

Glen said...

Any Errors messages ? are you using Exchange 2000 or 2003

Jakke said...

I have the problem that only my admin account has acces; so I can't use it from my workstation

I'm a good admin I have 2 accounts :)

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

I've had the script running on my Exchange 2003 PF server for about 20 minutes, and I still don't have any output. It's just hanging.

I should have to say that this is typical with anything I do with WMI :(

Glen said...

You might want to put a few wscript.echo in so you can see the process of the script and see if its the WMI section that causing issue. If you have a wider problem with WMI this script is not maybe a good way of trying to fix this problem. Start testing with a simple WMI script and then try to work out what the WMI issue is.

Cheers
Glen

Anonymous said...

Great blog, so much useful stuff, but why don't you just:

Email = "smtp:" & wscript.arguments(1)
ExchangeServer = wscript.arguments(0)
Set rootDSE = GetObject("LDAP://RootDSE")
domainContainer = rootDSE.Get("defaultNamingContext")
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "ADSDSOObject"
conn.Open "ADs Provider"

LDAPStr = "<LDAP://" & DomainContainer & ">;(&(objectCategory=publicfolder)(proxyAddresses=" & email & "));folderPathname;subtree"

Set rs = conn.Execute(LDAPStr)
If rs.RecordCount = 1 Then
wscript.echo rs.fields("folderPathname")
end if

Nagendra said...

I tried now and works like a charm!

Peter Richter said...

Any idea how to get this to work on Exchange 2007? The E2k7 Pub Folder tools are terrible!

Glen said...

Have a look at http://knicksmith.blogspot.com/2007/03/finding-public-folder-by-email-address.html

Cheers
Glen

Anonymous said...

Simply Type:

Get-MailPublicFolder "PublicFolderEmail@domain.com" | Get-PublicFolder

Glen Scales said...

This Post was created in 2006 and is for people using 2003 or under