sorting

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.


Script is based on function display:

function display( [string]$subject, [string]$color )  {
  $len = 20
  if ( $subject.length -lt 20 ){
    $toadd=20-$subject.length;
    for ( $i=0; $i -lt $toadd; $i++ )
    {
      $subject=$subject+" ";
    }
    $len = $subject.length
  }
  else { $len = 20 }

  $index=$index+1
  Write-host -ForegroundColor $color -nonewline " |" ((($subject).ToString()).Substring(0,$len)).ToUpper()
}

It takes two parameters:

  • subject – the Email subject
  • color – the color that should be used to display particular email subject

Here is how it looks during execution (sorry for cuts and blurs – censorship was necessary):

Powershell moving outlook emails


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

# 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"
      $objBalloon.BalloonTipTitle = "$title"
      $objBalloon.BalloonTipText = "$text"
      $objBalloon.Visible = $True

     # HOW LONG TO SHOW THE BALLOON
      $objBalloon.ShowBalloonTip(5000)
}

# DISPLAY FUNCTION

function display( [string]$subject, [string]$color )  {
  $len = 20
  if ( $subject.length -lt 20 )
  {
    $toadd=20-$subject.length;
    for ( $i=0; $i -lt $toadd; $i++ ){
      $subject=$subject+" ";
    }
    $len = $subject.length
  }
  else { $len = 20 }
  $index=$index+1
  Write-host -ForegroundColor $color -nonewline " |" ((($subject).ToString()).Substring(0,$len)).ToUpper()
}

# PROCESSING EMAILS

foreach ($Email in $Emails) {
  $index=$index+1
        # FOUR SUBJECTS PER LINE
  if ( ($index % 4) -eq 0 ) {  Write-host -nonewline -ForegroundColor DarkGray " |`n" }

  if ($Email.To -eq "Surname, Name" -and $Email.Subject -notmatch "SPAM") {
      $Email.Move($nagiosFolder) | out-null
      display  ([string]$Email.Subject ) ([string]"Cyan")
      continue
  }
  if ($Email.Subject -match "SiteScope Alert, error") {
      $Email.Move($pstSubFolder.Item("SITESCOPE")) | out-null
      display  ([string]$Email.Subject ) ([string]"Yellow")
      continue
  }
    # DEFAULT BEHAVIOR IF NO MATCH IN RULES ABOVE
    display  ([string]$Email.Subject ) ([string]"DarkGray")
}

Obrazek

Advertisements