2016-08-19 81 views
0

我在一个管理几百台服务器的团队工作。我们每个人对大约100台服务器承担主要责任。我是团队中的新成员,因此我在Outlook中制定了一个“MyServers”规则,它会发出特殊的声音,并将电子邮件移动到文件夹“MyServers”中,当电子邮件以其中一个服务器名称主题或身体。服务器来来往往,负责人偶尔会改变。我可以使用GUI来修改列表,但我想要做的是使用PowerShell根据来自我们的表上的SQL查询的数据集来修改服务器列表,这些数据集属于哪个列表。 (也可以在为别人掩盖时有所帮助使用PowerShell修改Outlook规则中的数组文本条件?

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)

最优:我想转到 “MyServers” 的规则,改变阵列,从它是一个新的数组从SQL表中获取列表后,我将使用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”规则,修改数组,并用新的规则覆盖旧规则。这是有问题的,因为它限制了选项,只能以编程方式创建一些条件和操作。

+0

您是否考虑过探索EWS托管API?我没有示例代码,但我已经使用它(在PowerShell中)非常成功地处理邮箱中的规则。 https://msdn.microsoft.com/en-us/library/office/dn481313%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396#bk_UpdateRulesEWSMA –

+0

@ChrisDent,没有,现在看着它。首先想到的是我不确定我能从SQL中获取数据,用PowerShell我应该能够使用SQL获取数据并将其操作到数组中,并将其保存到一个命令的Outlook中。 –

+0

无法以与Microsoft.Office.Interop.Outlook不同的方式从SQL中获取EWS。无论你的命令或脚本能做什么,建议只是为改变规则提供一个潜在的更好的界面。 –

回答

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() 
+0

测试成功,谢谢。只修改一条规则的标准,该规则在更新后按预期工作。 –