Having rules run on old mails noved to inbox

Status
Not open for further replies.

odoll

New Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
OL16 64b on W10 64b

Hello,

I have a little powershell script which moves items to a PST files standard inbox.
How can I achieve that those mails get picked up by the rule engine as if they would have had arrived as incoming (e.g. same as the ones which get downloaded from a pop3 mbx)?

Thanks a lot in advance.
 

odoll

New Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
Thx a lot Diane,

> The only way would be to use run rules now command.

I already came across hints like this, however I understood it the way as if you'd have to read all defined rules first and execute them on the item one after another?

> Otherwise rules only run on mail downloaded from the service, as the mail passes through the mapi subsystem

My script is passing the mails through the MAPI as well, so I thought there might be a hook to tell the engine "Hi, here's a new mail, please process your rules on it."

In a nutshell the script looks like this:
Code:
# generate Outlook objects
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$OL = New-Object -ComObject Outlook.Application
$NS = $OL.GetNS("MAPI")

# get the correct mailboxes
$MbxS = $NS.Folders | Where { $_.FolderPath -match "\\<account name>" }
$SrcFldr = $MbxS.Folders | Where { $_.Folderpath -match "Posteingang" }
$DstFldr = $NS.GetDefaultFolder(6)

# move all items from Source to Destination folder
$EMails = $SrcFldr.Items
ForEach ($EMail in $EMails){
  $EMail.Move($DstFldr) | Out-Null
  <start_rule_engine_on_mail_here>
}
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
you'd have to read all defined rules first and execute them on the item one after another?
Yeah, more or less, but it should be fast... unless you have a ton of rules. I would move the messages then run the rules on the inbox. It would be faster than running them on each message. I have a VBA version here - Run Rules Now using a Macro

if the rules are simple and you don't have many, you might be able file the messages as you move. (I have not tried converting a move macro to powershell.) The big thing is if you have a lot of rules with a lot of different types of conditions... the script gets heavy and slow as it checks each condition. In this case, running rules would be better. Rules such as 'move mail from sender display name to folder named for sender' would be fairly simple to convert. Keywords in the subject to folders named for keywords would be easy too. Converting a mix of these kinds of rules is where it slows as the scripts checks for matches.

My script is passing the mails through the MAPI as well
Outlook scripts always use mapi - you aren't passing it through the part that downloads messages.
 

odoll

New Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
Diane, thanks a lot for your hints!

Though I'm currently stuck with the following odd behaviour of the above little script, which however is a bit off-topic here.
Nevertheless I'm gonna mention it:

While working on a bigger number of mail items I noticed, that the ForEach loop doesn't process all items in the source folder, but only ~ half of them (to be precise, if the number N of items it odd, it processes (N+1)/2 items in one go, but leaves the remaining item in the folder.)

So the loop has some "binary based logarithmic attitude! ;-)

As an example: lets assume, if $EMails.count is 7, the ForEach ($EMail in $EMails){...} only works on 4 of the 7 items, leaving 3 in the folder.
During the next round it only moves 2 of the 3, finally the last one.

E.g. starting with 16 items it's 16 -> 8 -> 4 -> 2 -> 1 -> 0.

Am I doing something wrong here?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
During the next round it only moves 2 of the 3, finally the last one.
If you are moving or deleting, you need to count then step backwards through the messages - when you do this going forward, after moving an item, item #2 now becomes item #1, #3 is #2 and outlook moves on to #2.

For i = 1 to items.count step -1
' move or delete
next i
 

odoll

New Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
Thx Dianne, I was under the impression when defining

$EMails = $SrcFldr.Items

in Powershell $EMails would be a "static" list of all all objects in $SrcFldr.Items, but obviously it's dynamically changing while moving the items.

BTW: as using piping the objects into a ForEach-Object () loop as in

Code:
$SrcFldr.Items | ForEach-Object {
  $_.Move($DstFldr) | Out-Null
}
has the same "problem" I decided to do it in a while loop

Code:
while($SrcFldr.Items.Count -gt 0) {
   $SrcFldr.Items[1].Move($DstFldr) | Out-Null
}
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Since it doesn't create an actual list of message ids or other identifiable characteristics, it can't keep track of the messages and just goes by count (which remains static) - but the actual count keeps changing in outlook. Recounting after each move and is slower in VBA, that's why it's recommended to work backwards.
 

odoll

New Member
Outlook version
Outlook 2016 64 bit
Email Account
POP3
OK, thx for the recommendation. Replaced "1" by the count then, so it should always move off from the end, even a new item gets in.

Code:
while($SrcFldr.Items.Count -gt 0) {
   $SrcFldr.Items[$SrcFldr.Items.Count].Move($DstFldr) | Out-Null
}
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
dbjdbj Is it only me having a fun time with Outlook.com "beta" ? Using Outlook 3
O Importing Mbox - anyone out there having experience with this tool... Using Outlook 3
A Outlook 2019 having issues with 'people search' and not making suggestions in email To: etc Using Outlook 12
M I'm Having problems Moving Contacts to a New List Using Outlook 8
V Problem not having Contacts folder after importing emails from Yahoo. Using Outlook 0
B Trying to get old emails from a not working computer but having trouble. Using Outlook 6
J outlook having sort by date or from unable to display sender name. Using Outlook 2
M Having Calander send E-mails Using Outlook 3
B Having Recent Issues with Hotmail Connector? Using Outlook.com accounts in Outlook 17
O Exchange 2003 displaying HTML emails as having attachments Exchange Server Administration 7
D I am having the problem that Outlook 2010 is checking for email via send /receive approximately every thirty seconds. Using Outlook 2
N Stop accepting connections for sending e-mail from users not having a mailbox Exchange Server Administration 6
C Outlook 2007: Establishing a View By Category with contacts having multiple categories displayed properly Using Outlook 2
C Having problems with taking items out of one folder and sending them into inbox or sending items back to my e-mail address of Email removed for privac Using Outlook 2
B Having issues with the 'look' of Office 2010. Does not look right? Using Outlook 1
G I just purchased MS Office and am having a problem with Outlook 2010 Using Outlook 6
M Activesync having difficulty syncronizing large mailboxes Using Outlook 1
B How can I do what Business Contact Manager does without actually having the software? Using Outlook 1
P How to set up meeting while in Eastern Time Zone for a meeting I will be having in the Central Time Using Outlook 1
B Having trouble with an Outlook Profile. Can I uninstall Outlook 2010 and install it again? Using Outlook 11
J Having problems subscribing to my Outlook calendar online Using Outlook 3
F Why is an unmounted Exchange Server hosting mailboxes after having configured a DAG but haven't brou Exchange Server Administration 9
M im having trouble going from one mail server to another on my computer Using Outlook 1
E Having difficulty sending or receiving mail from the other Hotmail account I opened the error messag Using Outlook 1
A send a email, with out having to save contact details, in office 03 and 07 you could uncheck and disable email name check Using Outlook 1
S Outlook 2007 wont start, keep having error message. See below__ Using Outlook 2
S Forwarding mail without having to use the Delegate option Using Outlook 2
W Having issues with voting and tracking options Using Outlook 1
R accessing owa externally and having a cas array internally Exchange Server Administration 9
C Having issues with Google Desktop and Outlook 2010 mail: Using Outlook 1
B I am currently having problems / issues with MS Project 2010 and MS Outlook 2010 freezing Using Outlook 2
J I am having trouble opening opening links with in an e-mail. Using Outlook 2
M I am having trouble sending my e-mails in Outlook 2007. I can receive them, but not send. My internet provider says they can't help me. What to do? Using Outlook 6
V Outlook 2007 and mobile devices having trouble with Exchange 2007 Using Outlook 2
R Having trobule Mapping Hotmail to Outlook 2007 Using Outlook 2
A my account was activated without having an email account connected to it. What do I do? Using Outlook 2
L Having 2 Accounts in Outlook 2007 Using Outlook 1
D I cannot get Outlook 2010 to run without having an email account. Using Outlook 1
R Having a problem with a specific recurring appointment. Using Outlook 1
A calendar entries default to having alerts, even tho unchecked Using Outlook 2
M I am having lots of trouble sending my emails out thru microsoft 2003 Getting error message and then they time out. If I got out of computer and the Using Outlook 1
D Users having meetings overbooked in outlook 2003 Using Outlook 5
O Having name appear after typing a few letters in new email Using Outlook 1
A Having trouble receiving emails, people unable to reply to the ones I send them. Using Outlook 1
I Two users on one computer using Outlook 2007, having individual contacts, calender and email Using Outlook 2
S Having trouble setting up different email accounts in Outlook 2010. Using Outlook 3
K I am having problems seeing new emails on arrival in my Inbox and sent emails in the Sent Items fold Using Outlook 5
S Having trouble with email stuck in outbox Using Outlook 1
V I am having a hard time setting up my yahoo mail in Outlook. Please help! Using Outlook 2
W Unable to open Outlook due to a corrupt file wgeelan_msn1.ost. Inbox repair tool to diagnose and repair errors in the file. Having difficulty using in Using Outlook 2
Similar threads


















































Top