-1

I work on a team, who manage a few hundred servers. We each take primary responsibility for about 100 servers. I am the new person on the team, so I have a rule "MyServers" in outlook that makes a special sound and moves emails in to the folder "MyServers", when an email comes in with the name of one of my servers in the subject or body. Servers come and go, and the responsible person changes occasionally. I can use the GUI to modify the list, but what I want to do is use PowerShell to modify the list of servers based on a data set from a SQL query on our table of whom belongs to what. (also would be helpful when covering for someone else.)

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 Multiple Actions for one Outlook rule in Powershell has also been helpful.

I can find several examples of creating rules, mostly around email addresses. As I did more research (below) it seems like the I want to modify 'TextRuleCondition.Text' but I am not finding any example code that gets in to reading OR modifying rule conditions for a single existing rule.

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

Optimally: I would like to go to the "MyServers" rule and change the array, from what it is to a new array that I will build with PowerShell, after getting the list from a 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. 

Everything I have found so far programmatically creates an entire new rule from PowerShell. Nothing indicates if it is, or is not possible to modify an existing rule. My Plan "B" would be to read the existing "MyServers" rule, modify the array, and overwrite the old rule with a new one. This is problematic as it limits options, only some conditions and actions can be created programmatically.

Community
  • 1
  • 1
James Jenkins
  • 1,954
  • 1
  • 24
  • 43
  • Have you considered exploring the EWS managed API? I haven't sample code, but I've used this (in PowerShell) to manipulate rules in a mailbox very successfully. https://msdn.microsoft.com/en-us/library/office/dn481313%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396#bk_UpdateRulesEWSMA – Chris Dent Aug 19 '16 at 13:08
  • @ChrisDent, Had not, looking into it now. First thought is not sure I can get the data from SQL with it, with PowerShell I should be able to go SQL get the data and manipulate it into the arrays and save it to outlook in one command. – James Jenkins Aug 19 '16 at 13:14
  • You can't get from SQL with EWS in the same way that you cannot with Microsoft.Office.Interop.Outlook. Whatever your command or script does can though, the suggestion was only to provide a potentially better interface for changing rules. – Chris Dent Aug 19 '16 at 13:30
  • @ChrisDent EWS looks like it has some potential, I am not currently using C# or XML, so would need to work on my coding skills with them, I will consider EWS as my **Plan "C"** – James Jenkins Aug 19 '16 at 14:09
  • You can use the EWS managed API in PowerShell directly, no need to use either C# or XML. It's little different from working with the interop class above, but for finding examples. – Chris Dent Aug 19 '16 at 14:12

1 Answers1

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()
MaddHatter
  • 212
  • 2
  • 10