我尝试使用Powershell脚本在Outlook客户端中创建收件箱规则,但似乎无法存储路径。
$Outlook = New-Object -ComObject Outlook.Application
$mbs = $outlook.session.folders
Add-Type -AssemblyName microsoft.office.interop.outlook
$olRuleType = “Microsoft.Office.Interop.Outlook.OlRuleType” -as [type]
$rules = $outlook.session.DefaultStore.GetRules()
$rule = $rules.Create(“move appointments”,$olRuleType::OlRuleReceive)
$FromCondition = $rule.Conditions.From
$FromCondition.Enabled = $true
$FromCondition.Recipients.Add(“test@test.com”)
$fromCondition.Recipients.ResolveAll()
$MoveRuleAction = $rule.actions.MoveToFolder
$MoveRuleAction.Folder = $mb[1].folders.item("MyTargetFolder")
$MoveRuleAction.Enabled = $true
$rules.Save()
我在运行脚本时收到此错误:
One or more rules cannot be saved because of invalid actions or conditions.
At line:1 char:1
+ $rules.Save()
+ ~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
我验证了可以使用$ mbs [1] .folders.item(“ MyTargetFolder”)访问正确的文件夹,并且没有收到任何错误消息,但是$ MoveRuleAction的父属性仍然为空。我还尝试了其他文件夹(如收件箱)的操作,但效果不佳。
关于如何创建此规则的任何建议?
谢谢!
编辑:这就是$ mbs [1] .folders.item(“ MyTargetFolder”)的样子:
Application : Microsoft.Office.Interop.Outlook.ApplicationClass
Class : 2
Session : System.__ComObject
Parent : System.__ComObject
DefaultItemType : 0
DefaultMessageClass : IPM.Note
Description :
EntryID : 000000007FBA7FDCC087A94D88906201B104868D010090B072A2594AD64FAA02EC7C88654D940195F69A807A0000
Folders : System.__ComObject
Items : System.__ComObject
Name : MyTargetFolder
StoreID : 0000000038A1BB1005E5101AA1BB08002B2A56C20000454D534D44422E444C4C00000000000000001B55FA20AA6611
CD9BC800AA002FC45A0C00000063687269737469616E2E6275636877616C644064652E7475762E636F6D002F6F3D54
55562F6F753D45786368616E67652041646D696E6973747261746976652047726F7570202846594449424F48463233
5350444C54292F636E3D526563697069656E74732F636E3D43687269737469616E204275636877616C6431653100E9
4632F44E0000000200000010000000630068007200690073007400690061006E002E00620075006300680077006100
6C0064004000640065002E007400750076002E0063006F006D0000000000
UnReadItemCount : 0
UserPermissions : System.__ComObject
WebViewOn : False
WebViewURL :
WebViewAllowNavigation : True
AddressBookName :
ShowAsOutlookAB : False
FolderPath : \\test@test.com\MyTargetFolder
InAppFolderSyncObject : False
CurrentView : System.__ComObject
CustomViewsOnly : False
Views : System.__ComObject
MAPIOBJECT : System.__ComObject
FullFolderPath : \\test@test.com\MyTargetFolder
IsSharePointFolder : False
ShowItemCount : 1
Store : System.__ComObject
PropertyAccessor : System.__ComObject
UserDefinedProperties : System.__ComObject
编辑:我忘了更改变量名,所以它不起作用,现在我感谢Ranadip Dutta的代码解决了它。这是工作代码:
$olRuleType = “Microsoft.Office.Interop.Outlook.OlRuleType” -as [type]
$rules = $outlook.session.DefaultStore.GetRules()
$rulefound = $false
foreach($r in $rules)
{
if($r.name -eq "move appointments"){
$rulefound = $true
}
}
if($rulefound)
{
}
else
{
$rule = $rules.Create(“move appointments”,$olRuleType::OlRuleReceive)
$FromCondition = $rule.Conditions.From
$FromCondition.Enabled = $true
$FromCondition.Recipients.Add(“test@test.com”)
$fromCondition.Recipients.ResolveAll()
$MoveRuleAction = $rule.actions.MoveToFolder
$MoveRuleAction.Enabled = $true
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember(
"Folder",
[System.Reflection.BindingFlags]::SetProperty,
$null,
$MoveRuleAction,
$mbs[1].folders.item("MyTargetFolder"))
$rules.Save()
}