Tuesday, January 22, 2013

outlook rules with powershell [without 32 KB storage limit]

Entry moved here: http://wondershell.wordpress.com/2014/04/09/outlook-rules-with-powershell-script-moving-emails-to-pst-file/


Have you ever reached the size limit of the rules that you can setup in Microsoft Outlook? Well, I did. By default it is 32 Kb which can be extended to 256 Kb. Still, if you receive couple of hundreds emails per day - like myself - maybe you will appreciate the fact that you don't have to be limited to mentioned thresholds. There is a fairly simple solution and it's name is - Powershell.
Below you will find a powershell script with a couple of example rules and a bonus function for displaying balloon tooltips. It is designed to work with PST file but if you have large mailbox quota you don't have to move emails outside of your exchange mailbox. 

This is how it will look like in console:

Here is how balloon tooltip will look like:

If You do not want to manually start the script you can use scheduled task and batch file similar to this one:

  @ECHO OFF
  TITLE "RULES"
  COLOR 0A
  CLS
  MODE CON COLS=30 LINES=10
  CD "D:\"
  powershell.exe .\RULES.ps1 >> rules.log

Or, if You do not want to see anything at all, You can use vbscript like this one:

  Dim shell, command
  command = "powershell.exe -nologo D:\RULES.ps1"
  Set shell = CreateObject("WScript.Shell")
  shell.Run command,0

Additionally, You can create hyperlink to one of those files (batch or vbscript) and associate it with a button in Outlook GUI:


Script:

# VARIABLES #

 # CREATE OUTLOOK INSTANCE
        $outlook = New-Object -comobject outlook.application
        $namespace = $outlook.GetNameSpace("MAPI")

 # PATH TO PST FILE
        $pstPath = "D:\PST\ALERT.pst"

 # ACCESS THE PST FILE AND MAP DESIRED FOLDERS
        $pst = $namespace.Stores | ?{$_.FilePath -eq $pstPath}
        $pstRoot = $pst.GetRootFolder()
        $pstFolders = $pstRoot.Folders
        $nagiosFolder = $pstFolders.Item("NAGIOS")

 # DEFAULT FOLDER 6 IS INBOX, 3 IS DELETED ITEMS
        $DefaultFolder = $namespace.GetDefaultFolder(6)
        $InboxFolders = $DefaultFolder.Folders
        $DeletedItems = $namespace.GetDefaultFolder(3)

        $Emails = $DefaultFolder.Items

# BALLOON FUNCTION

function balloon([string]$text, [string]$title)
{
        if ($objBalloon)
        {
         # delete existing balloon #
                $objBalloon.Dispose()
        }

                [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
                $objBalloon = New-Object System.Windows.Forms.NotifyIcon
                $objBalloon.Icon = "C:\Windows\ServicePackFiles\i386\msnms.ico"

         # Info, Warning and Error values are allowed
                $objBalloon.BalloonTipIcon = "Error"

         # Put what you want to say here for the Start of the process
                $objBalloon.BalloonTipTitle = "$title"
                $objBalloon.BalloonTipText = "$text"
                $objBalloon.Visible = $True

         # How long should the balloon be visible
                $objBalloon.ShowBalloonTip(5000)
}

# PROCESSING EMAILS

Foreach ($Email in $Emails)
{
        # UNCOMMENT NEXT LINE IF YOU WANT TO SEE
        # ALL EMAILS THAT ARE BEING REVIEWED
        # Write-host " " $Email.SentOn " :`t" $Email.Subject

        # MOVING TO NAGIOS FOLDER # MATCHING BY SUBJECT

        IF ($Email.Subject -match "Nagios Critical Alert")
        {
                $Email.Move($nagiosFolder) | out-null
                Write-host -ForegroundColor Green " " $Email.SentOn " :`t" $Email.Subject

                # DISPLAY BALLON
                balloon ([string]$Email.Subject) ([string]$Email.SentOn)
        }
        IF ($Email.Subject -match "Nagios Alert" -or $Email.Subject -match "Nagios Warning" )
        {
                $Email.Move($nagiosFolder) | out-null
                Write-host -ForegroundColor Green " " $Email.SentOn " :`t" $Email.Subject
        }

        # MOVING TO DELETED ITEMS # MATCHING BY SENDER

        IF ($Email.SenderEmailAddress -match "/O=CORPORATE/OU=GROUP/CN=RECIPIENTS/CN=SOME")
        {
                # MARKING AS UNREAD (TO SEE HOW MANY ITEMS HAVE BEEN DELETED)
                $Email.UnRead = $True

                # MOVE
                $Email.Move($DeletedItems) | out-null
                Write-host -ForegroundColor Red " " $Email.SentOn " :`t" $Email.Subject
        }
        IF ($Email.SenderEmailAddress -match "spammer@ads.com")
        {
                $Email.Move($DeletedItems) | out-null
                Write-host -ForegroundColor Red " " $Email.SentOn " :`t" $Email.Subject
        }
}

No comments:

Post a Comment