使用 PowerShell 修改 Outlook 规则中的数组文本条件?

Modify the array text condition in an Outlook rule with PowerShell?

我在一个管理数百台服务器的团队工作。我们每个人主要负责大约 100 台服务器。我是团队中的新人,所以我在 Outlook 中有一个规则 "MyServers",当收到一封电子邮件时,它会发出特殊的声音并将电子邮件移至文件夹 "MyServers"我在主题或正文中的服务器。服务员来来往往,负责人也时有变动。我可以使用 GUI 修改列表,但我想做的是使用 PowerShell 修改服务器列表,该列表基于来自 SQL 查询的数据集 table of who belongs what . (在为别人掩护时也会有所帮助。

Per PowerShell - Managing an Outlook Mailbox with PowerShell, By Joe Leibowitz, March 2013 it is possible in theory. That article and the post Hey, Scripting Guy! How Can I Tell Which Outlook Rules I Have Created? December 15, 2009 by ScriptingGuy1 have taught me how to get outlook files into PowerShell to read and or write. The post 也很有帮助。

我可以找到几个创建规则的示例,主要是围绕电子邮件地址。随着我做更多的研究(如下),我似乎想修改 'TextRuleCondition.Text' 但我没有找到任何示例代码来读取或修改单个现有规则的规则条件。

  1. Specifying Rule Conditions
  2. TextRuleCondition Object (Outlook)
  3. TextRuleCondition.Text Property (Outlook)

最佳: 我想转到 "MyServers" 规则并更改数组,从原来的数组更改为我将使用 PowerShell 构建的新数组,从 SQL table.

获取列表后
##source https://blogs.technet.microsoft.com/heyscriptingguy/2009/12/15/hey-scripting-guy-how-can-i-tell-which-outlook-rules-i-have-created/
## Gets outlook rules on PC
#Requires -version 2.0
Add-Type -AssemblyName microsoft.office.interop.outlook 
$olFolders = “Microsoft.Office.Interop.Outlook.OlDefaultFolders” -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace  = $Outlook.GetNameSpace(“mapi”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderInbox)
$rules = $outlook.session.DefaultStore.<Some code here gets TextRuleCondition.Text for just MyServers>
$rules ## this displays the current value
##More code inserts the array that I built earlier (not actually built, yet as I don't know what it should look like)
$rules.Save() ## this saves the changes. 

到目前为止,我发现的所有内容都以编程方式从 PowerShell 创建了一个全新的规则。没有任何内容表明是否可以修改现有规则。我的 计划 "B" 将读取现有的 "MyServers" 规则,修改数组,然后用新规则覆盖旧规则。这是有问题的,因为它限制了选项,只能以编程方式创建某些条件和操作。

#setup
Add-Type -AssemblyName microsoft.office.interop.outlook 
$olFolders = “Microsoft.Office.Interop.Outlook.OlDefaultFolders” -as [type]
$outlook = New-Object -ComObject outlook.application
$namespace  = $Outlook.GetNameSpace(“mapi”)

#get all of the rules
$rules = $outlook.Session.DefaultStore.GetRules()

#get just the rule I care about
$myRule = $rules | ? { $_.Name -eq 'My Awesome Rule' }

#build my new array of text conditions
$textValues = @('ServerA', 'NewServerB')

#replace the existing value on my rule (this is assuming you are using BodyOrSubject, you can substitute Body, Subject, etc)
$myRule.Conditions.BodyOrSubject.Text = $textValues

#save all the rules
$rules.save()