Search code examples
powershelloutlookpowershell-2.0outlook-2010

Error creating Outlook Rules in Powershell


I've been messing around with Outlook objects for Powershell. I wanted to create Outlook Rules and I got it to work. Layed the project aside for a while. When I picked it back up I was unable to create Outlook rules. Note that I did NOT change anything in the code.

Code generating Outlook Rule

 $rules = $ol.Session.DefaultStore.GetRules()

 $rule = $rules.Create("spamfilter",[Microsoft.Office.Interop.Outlook.OlRule Type]::olRuleReceive)

    $condition = $rule.Conditions.SenderAddress
    $condition.Enabled = $true
    $condition.Address = @("<sender>")
    $action = $rule.Actions.MoveToFolder
    $action.Enabled = $true

    [Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember("Folder",[System.Reflection.BindingFlags]::SetProperty,$null,$action,$junk)

    $rules.Save()

The error

Exception calling "InvokeMember" with "5" argument(s): "Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))" At line:22 char:5

  • [Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember("Folde ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : COMException

Someone please help me. What seems to be the issue? I'm lost, thanks.


Solution

  • I found the problem. I defined the $junk variable using Redemption outlook library. This way it's not a legit MAPI object so it doesn't get recognized as a valid parameter.

    Instead of defining $junk = $routlook.GetDefaultFolder(23), I had to use $junk = $ol.Session.DefaultStore.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderJunk)

    But ofcourse I did not provide this information in the question because I didn't think it seemed relevant as nothing else in my script makes a problem of how this folder is defined.