When everything started to work (scripts are like that – it’s all or nothing) everything seemed fine until I used the script in a batch where I created many rules on a specific mailbox store.
The second code block I set the Folder property on the MoveToFolder action object. There are other ways to get a handle on a folder in Exchange/Outlook, but I wanted to show an alternate way of doing this in this sample. NET to first get a property from a ComObject.
In the first code block below I use the InvokeMember method that is part of reflection for types in.
NET it is possible to get and set properties on ComObjects and this turned out to be the solution to my problem. NET friend – Reflection! By using reflection on objects in. I came up with the idea that maybe the solution would be not to cast the ComObject but instead try to interact with it somehow, and for this we have our. I tried to get a handle on the desired folder in many different and this part was actually quite easy, but the problem still remained: It was not possible to cast the ComObject to a MAPIFolder. No matter how I tried to cast the ComObject to a MAPIFolder the script constantly failed. When I tried to set the Folder property on the MoveToFolder object I got an error stating that I was trying to set a property that expected an object that implemented the interface MAPIFolder to a ComObject which apparently doesn’t do that.Įxception setting "Folder": "Cannot convert the "System._ComObject" value of type "System._ComObject#" to type ".MAPIFolder"." After some additional investigation I found out that the string array needed to contain more than one item which means that if you have an array with two or more items the script started to work. I found out that the subject field expected an array of type string, but by some strange reason I didn’t get it to work, even tough I Reflected the PIA (Primary Interop Assembly) for Microsoft Outlook and checked exactly what types the class expected and tried to cast the variable in all possible ways available. The first problem I ran into was with the criterion that looked for certain words in the Subject field. $()Īdd_outlook_rule "This is my custom rule" "M圜ustomFolder" now for the problems I ran into… Problem number 1 #$MoveTarget = $namespace.getDefaultFolder( # Uncomment the row below if you want to use the DeletedItems $MoveTarget = $namespace.getFolderFromID($d) $rule = $rules.Create($RuleName,$olRuleType::OlRuleReceive) $inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox) $namespace = $Outlook.GetNameSpace("MAPI")
$outlook = New-Object -ComObject outlook.application #Īdd-Type $class -ReferencedAssemblies Īdd-Type -AssemblyName ( FROMEMAIL) and the subject contain the words ‘Hello’ and ‘World’ (SUBJECTWORDARRAY), the message should be forwarded to (FORWARDEMAIL) and moved to the M圜ustomFolder (MOVETOFOLDER) folder which is a subfolder to the Inbox folder. The script below creates a new rule in Outlook (I’m currently running Outlook 2010) that checks the sender of the message and looks for certain words in the subject field. So, in order to solve my problem I had to do a lot of looking around at the forums on the web. Ok, do this might seem to be a trivial task, but as always when you think something seems to be easy you run into problems, especially when working with COM-objects/Automation. it’s easy to see the tremendous power in PowerShell you can say that it’s the command-prompt on steroids :-) I’m lovin’ it! The task – Creating an Outlook rule using PowerShell
Being a script fanatic using DOS instead of Windows, VBScript and JScript, Console-apps in favor to Windows apps, regex, etc. I recently started using Microsoft PowerShell to carry out administrative tasks and it didn’t take long for me to fall in love with it.