Powershell 中一个 Outlook 规则的多个操作

Multiple Actions for one Outlook rule in Powershell

我对 Powershell 中的 Outlook 规则有疑问。我编写了一些代码,成功地将来自特定发件人的任何传入电子邮件存储到已删除项目文件夹中。我这样做是因为当邮件进入垃圾文件夹时,垃圾文件夹仍然有邮件的计数器令牌,所以最后它会说我在垃圾文件夹中有10封邮件。

我想通过将来自该发件人的传入邮件扔到已删除邮件文件夹并将邮件标记为 "read" 来避免这种情况,这样我就不会在已删除邮件文件夹中看到混乱情况.

问题真的是:

到目前为止我的代码:

$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNamespace("MAPI")

$olFolders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders"
$outlook = New-Object -ComObject outlook.application
$namespace  = $Outlook.GetNameSpace("MAPI")

$inBox = $ns.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$deleted = $ns.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderDeletedItems)

$rules = $outlook.session.DefaultStore.GetRules()
$rule = $rules.create("Move mail: to DeletedItems", [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive)

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

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

$rules.Save()

这段代码目前有效。

请帮忙。 谢谢!

Can I add multiple actions to the same outlook rule in powershell? if so, how?

花了一点时间,但我得到了一个工作测试,该测试使用应用于单个规则的多个操作。这实际上很容易,您只需要重复您已经完成的步骤并创建一个不同的动作变量。

在我的示例中,仅显示代码的末尾,我添加了一个操作以在新项目警报中显示一条消息 window。

...
$action = $rule.Actions.MoveToFolder
$action.Enabled = $true 

$anotherAction = $rule.Actions.NewItemAlert
$anotherAction.Text = "I am awesome!"
$anotherAction.Enabled = $true 

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

$rules.Save()

您很可能已经尝试过类似的操作。如果没有,您需要了解一个重要的参考资料。

What is the syntax / code for the "run script" action?

这是您无法根据 this reference for Office 2007 or this one for Office 2010/2013 以编程方式设置的操作之一。这些表格相似且相当大,无法包含在此处,但我将在您的第二个项目符号中引用该表格。

Action                                              : Start a script
Constant in olRuleActionType                        : olRuleActionRunScript
Supported when creating new rules programmatically? : No
Apply to olRuleReceive rules?                       : Yes
Apply to olRuleSend rules?                          : No

还有其他一些地方您受到限制。所以当你制定规则时,你需要记住这一点。