【问题标题】:Modify the array text condition in an Outlook rule with PowerShell?使用 PowerShell 修改 Outlook 规则中的数组文本条件?
【发布时间】:2016-12-26 15:52:36
【问题描述】:

我在一个管理数百台服务器的团队中工作。我们每个人主要负责大约 100 台服务器。我是团队中的新人,所以我在 Outlook 中有一个规则“MyServers”,当一封带有我的服务器名称的电子邮件进入时,它会发出特殊的声音并将电子邮件移动到文件夹“MyServers”中主体或主体。服务器来来去去,负责人不时更换。我可以使用 GUI 来修改列表,但我想要做的是使用 PowerShell 根据我们的表上的 SQL 查询中的数据集修改服务器列表,该数据集属于谁。 (在替别人掩护时也会很有帮助。

根据PowerShell - Managing an Outlook Mailbox with PowerShell, By Joe Leibowitz, March 2013,理论上是可能的。那篇文章和Hey, Scripting Guy! How Can I Tell Which Outlook Rules I Have Created? December 15, 2009 by ScriptingGuy1 的帖子教会了我如何将 Outlook 文件放入 PowerShell 中进行读写。 Multiple Actions for one Outlook rule in Powershell 的帖子也很有帮助。

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

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

最佳:在从 SQL 获取列表后,我想转到“MyServers”规则并将数组从原来的数组更改为我将使用 PowerShell 构建的新数组表。

##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”规则,修改数组,并用新规则覆盖旧规则。这是有问题的,因为它限制了选项,只能以编程方式创建一些条件和操作。

【问题讨论】:

  • 您是否考虑过探索 EWS 托管 API?我没有示例代码,但我已经使用它(在 PowerShell 中)非常成功地操作邮箱中的规则。 msdn.microsoft.com/en-us/library/office/…
  • @ChrisDent,没有,现在正在调查。第一个想法是不确定我是否可以使用它从 SQL 中获取数据,使用 PowerShell 我应该能够使用 SQL 获取数据并将其操作到数组中并在一个命令中将其保存到 Outlook。
  • 您无法使用 EWS 从 SQL 中获取信息,就像使用 Microsoft.Office.Interop.Outlook 一样。不管你的命令或脚本做什么,建议只是为更改规则提供一个可能更好的界面。
  • @ChrisDent EWS 看起来有一些潜力,我目前没有使用 C# 或 XML,因此需要与他们一起提高我的编码技能,我会将 EWS 视为我的 计划“ C"
  • 您可以直接在 PowerShell 中使用 EWS 托管 API,无需使用 C# 或 XML。它与使用上面的互操作类几乎没有什么不同,但用于查找示例。

标签: email powershell outlook outlook-2010


【解决方案1】:
#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()

【讨论】:

  • 测试成功,谢谢。仅修改了一个规则的条件,更新后规则按预期工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 2016-06-13
  • 2016-09-28
相关资源
最近更新 更多