Monday, March 11, 2013

outlook rules (moving emails) with powershell - upgraded version

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

I've modified previous script a bit, so that the code would be easier to understand and modify. 

This version 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 like now (sorry for cuts and blurs - had to do it):



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")
}


No comments:

Post a Comment