Link to home
Start Free TrialLog in
Avatar of jamorlando
jamorlandoFlag for United States of America

asked on

Create Outlook "Rules And Alerts" with Powershell

Hi,
I'm trying to figure out how to create new message rules in Outlook using Powershell 2.0.  I've tested this in both 2007 and 2010, and can't get it to work.

I am referring to the script
CreateOutLookRule.ps1
http://blogs.technet.com/b/heyscriptingguy/archive/2009/12/16/hey-scripting-guy-december-16-2009.aspx

It fails at the line:
$MoveRuleAction.Folder = $Movetarget

with the error:
Exception setting "Folder": "Cannot convert the "System.__ComObject" value of type "System.__ComObject#{00063006-0000-0000-c000-000000000046}" to type "Microsoft.Office.Interop.Outlook.MAPIFolder"."

Even at the top of this article he talks about how people were getting type mismatch errors.

I also tried the script at this link and get the same type mismatch error:
http://www.eggheadcafe.com/software/aspnet/29542597/can-not-convert-systemcomobject-to-microsoftofficeinteropoutlookmapifolder.aspx

Both issues seem to currently be unresolved.

Can anyone see if they can test these two scripts and come up with a solution?

Thanks,
Jamie
Avatar of daveTechSearch
daveTechSearch
Flag of Canada image

Just looking at the first link (hey scripting guy). I'm using Outlook 2010.
Looking at this line:
$MoveTarget = $inbox.Folders.item("bob")
Run the script once (get errors, etc), then enter:
$inbox.Folders | get-member
I don't see a property or method called "item".
Avatar of jamorlando

ASKER

Dave, but if after that you were to run:
$inbox.Folders.item("bob") | fl

It returns data ... presumably the folder data.
you are correct. Odd. It should just work...
It seems to work for some, but not for others.

To clarify my original question, I just need a way to script out adding new rules.
Even if there was a way to do this from the Exchange side of things, that would be acceptable.

I'm surprised there aren't more article about this online.
Referring to the CreateOutLookRule.ps1 script:

If you run:
$namespace.GetType

You can see the type is NameSpaceClass

In the code, there is the line:
$inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox)

According to MSDN, this records type Microsoft.Office.Interop.Outlook.MAPIFolder
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.namespaceclass.getdefaultfolder.aspx

However when I run:
$inbox.GetType()

I see the non-descript __ComObject type, which I believe is the root of the problem.

Can any hardcore PowerShell nerds jump in and help me out?
I've only been dabbling in PS for a couple months.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of jamorlando
jamorlando
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Other thread solved my problem.