Thursday, February 24, 2005

Mail-Enabling a Public Folder via WMI (Exchange 2003)

One of the new WMI classes that comes with Exchange 2003 is the Exchange_PublicFolder Class. This is actually pretty useful when it comes to managing public folders via Code. For instance being able to propagate the folder settings to all of child subjfolers http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_wmiref_me_exchange_publicfolderpropagatesettings.asp . The other cool thing with this particular class is that a lot of the WMI properties are Read-Write. This means you can actually change setting and manipulate different aspect of public folders which you would usually need to use CDOEXM for. One of these is being able to mail-enable/disable a public folder. An example of mail-enabling a public folder via WMI would look like this. First you need to query the instances of public folders to find the folder you want to modify. I’ve used the Path property to search for the folder which is the normal path to the public folder but without the root suffix so if my public folder was http://server/public/myfolder my search string would need to be /myfolder/ (the end / is import). After that once you have made your modifications you need to call the Put_() method to commit the changes

The code looks like

On Error Resume Next
Dim cComputerName
Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_PublicFolder"
cComputerName = "servername"
cPublicFolderPath = "/foldertomailenable/"

strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//"& _
cComputerName&"/"&cWMINameSpace
Set objWMIServices = GetObject(strWinMgmts)
Set objPubInstances = objWMIServices.ExecQuery ("Select * From
Exchange_PublicFolder Where Path='" & cPublicFolderPath & "'")

For Each objExchange_PublicFolder in objPubInstances
WScript.echo objExchange_PublicFolder.Path
WScript.echo objExchange_PublicFolder.IsMailEnabled
objExchange_PublicFolder.IsMailEnabled = true
objExchange_PublicFolder.Put_()
Next

11 comments:

Ulf said...

Good article, but i have been wondering about how to create a public folder with code. Would it be possible to create a public folder via the Exchange_publicfolder wmi class?

Glen said...

You need to look at using CDOEX or WebDAV to create folders (or CDO 1.2) there are sample of how to do this in the Exchange SDK see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_exch2k_creating_folders.asp

CC said...

Thanks for this code. I am having trouble getting it to show me properties for subfolders unless I substitute the query line (ExecQuery) for
'Set objPubInstances = objWMIServices.InstancesOf("Exchange_PublicFolder"). If I indicate a path like "/ACAD Units/Test/Dump/", I will (after a great length of time) get the properties of Dump but nothing udnerneath it. It could be that it just takes excessively long (I have an exit line in my text which never executes and I'm never returned to a cursor), but I can't udnerstand why.

Curiously, I also never see it report a "1" - only 0 or -1.

Any thoughts?

Glen said...

Its might be worth you downloading the WMI SDK and installing it on a machine somewhere so you can do some testing. There is a tool that is part of the WMI SDK called the CIM studio. This lets you browse the WMI classes on a Exchange server. What is good about this tool is that it allows you to make adhoc queries and also browse all the instances of public folder. But if you look at the instance of that folder in the CIM studio you should be able to see what the correct path should be to query the folder. If you have a large number of subfolders then doing a semi synchronous query is a better idea eg add ,,48 after the query which should help with speed and time outs.

In VB the enumerations of True and False are True is 0 and False is -1 so what you seeing is okay.

Anonymous said...

How would that translate to Powershell? I'm having the hardest time using the "set" portion of the ismailenabled property...

Hector Hernandez

Glen said...

Hi Hector

The following works for me okay remember this is Exchange 2003 only (not 2007)

$servername = "servername"
$pfname = "/foldername/"
$filter = "Path='" + $pfname + "'"
$pf = get-wmiobject -class Exchange_Publicfolder -Namespace ROOT\MicrosoftExchangev2 -ComputerName

$servername -filter $filter
"Mail Enabled Status : " + $pf.isMailEnabled
$pf.isMailEnabled = $true
[Void]$pf.put()

Rob said...

Do you now recommend using WebDav in place of ExOleDb generally ?

Glen said...

No WebDAV is a remote protocol and Exoledb can only be used locally. If you running the code locally on the Exchange server then i would use Exoledb if its remote you need to use WebDAV (or Mapi)

Cheers
Glen

Carlos said...

I know this is an old post, but hopefully you can help. Is there a way to go DIRECTLY to the folder you want to manipulate rather than do a query to find it? The query is quite slow and CPU usage from lsass.exe and exmgmt.exe is quite high while the query is executing. For my purposes I will always know the path to the folder I want to work on, so it would be great if there was a way to just manipulate the folder directly as opposed to searching for it manually.

Thanks for any advice!

Carlos

Glen said...

You can but you need to know the direct path in WMI which is based on the Key values. The tools from the WMI SDK can help you work out what the full path value you need to use is. I find this a little impractical to use but it may work for you.

Nikhil said...

Is there a way to get the email address of public folder either using vbscript or Powershell ?