locked
Powershell and Outlook Moving Messages RRS feed

  • Question

  • I am trying to automate the moving of messages with powershell within outlook. I want messages to stay in my inbox for a specific period of time and then when the script runs, move them to another folder based on some stipulations in the script.

    The script runs ok, but only seems to move a handful of messages at a time. I have to run it multiple times to get it to really move all of the messages. To me, that means it may be crashing without throwing an error.

    The second problem I am having is that the script is changing the received date of the messages to when it actually moves them.

    Can I get some help on this?!

    Here is the code:

    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")
    $inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox)
    $MoveTarget = $inbox.Folders.item("SCOM Alerts")
    $onedayback = (get-date).AddDays(-1)
    $items = $inbox.items
    $filter = "[ReceivedTime] > '#07/28/2010#'"
    $inbox = $namespace.GetDefaultFolder($olFolders::olFolderInbox).Items.Restrict($filter)
    $inbox.count
    foreach ($Message in $inbox) {
    
    if ($Message.SenderName -match "SCOM-Prod") {
    $Message.SenderName
    [void]$Message.Move($MoveTarget) | out-null
    }
    
    }

    Sunday, August 1, 2010 4:24 AM

Answers

  • In your foreach ($Message in $inbox) { loop, you move the $Message, and $inbox.count decriments.

    You run into the problem of $inbox no longer having valid data.

    Say your initial count is 10 items.

    The first time through, you move item #1 of 10 - $inbox.count is 9
    The second time through, you move #2 of 10 - $inbox count is 8
    3 of 10 - $inbox count is 7
    4 of 10 - $inbox count is 6
    5 of 10 - $inbox count is 5
    6 of 10 fails, as $inbox.count is 5, not 6.

    Try this:

    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")
    $inbox = $namespace.getDefaultFolder($olFolders::olFolderInbox)
    $MoveTarget = $inbox.Folders.item("PowerShell")
    $onedayback = (get-date).AddDays(-10)
    $items = $inbox.items
    $filter = "[ReceivedTime] > '#07/28/2010#'"
    $inbox = $namespace.GetDefaultFolder($olFolders::olFolderInbox).Items.Restrict($filter)
    $inbox.count
    $messages = $inbox.Restrict("[From] = SCOM-Prod")
    for ($inc = $messages.count; $inc -gt 0 ; $inc --) {
    Write-Output $messages.Item($inc).Subject
    [void]$messages.Item($inc).Move($MoveTarget) | out-null
    }

     

     


    http://unlockpowershell.wordpress.com
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Proposed as answer by LikeToCode Monday, August 2, 2010 8:41 PM
    • Marked as answer by IamMred Tuesday, August 17, 2010 4:12 PM
    Monday, August 2, 2010 2:54 PM