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.
 
The only way would be to use run rules now command. Otherwise rules only run on mail downloaded from the service, as the mail passes through the mapi subsystem.


Sent from my iPad using Tapatalk
 
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>
}
 
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.
 
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?
 
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
 
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
}
 
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.
 
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.
Similar threads
Thread starter Title Forum Replies Date
R Outlook 2021 Having problem setting up outlook 2021 with windows 11. I have 3 gmail accounts and I want the 3 gmail, emails to merge into the same outlook input. Using Outlook.com accounts in Outlook 0
e_a_g_l_e_p_i Is anyone else having problem conneccting to gmail? Using Outlook 27
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
N Having Shared Calendar shift to Current day Using Outlook 0
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 2
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
T Get rid of having to type verification characters to send mail BCM (Business Contact Manager) 2
M having trouble importing excel data into outlook contacts BCM (Business Contact Manager) 1
A Contact Manager Unable to Initialize...after having tested Office BCM (Business Contact Manager) 1
R How to simplify rules Using Outlook 2
J Recover server side rules from OST/PST without access to the server Using Outlook 2
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
K Multiple Rules on Single Email Using Outlook 2
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
R rules not working - done troubleshooting Using Outlook 0
P Outlook 2013 All imported Mail Rules in error when imported into new profile Using Outlook 5
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5
Wayne Outlook locks up when opening "Manage Rules & Alerts" Using Outlook 7
T "Words In Recipient's Address" Rule Causes Outlook To Stop Processing Rules Using Outlook 3
Retired Geek Junk Folder Clean Up Rules Exchange Server Administration 1
B Setting rules Using Outlook 1
A rules not working Using Outlook 2
M Rules and autoreply Using Outlook 1
N Outlook rules don't create a copy for bcc'ed emails Using Outlook 3
O How to recover rules after switch from POP3 to IMAP Using Outlook 2
N Focussed Inbox Rules Failing Using Outlook 5
N Copies of emails from rules have wrong from: account Using Outlook 0
L Office 365 cloud based rules Using Outlook 1
K Outlook Rules: Move a Copy Using Outlook 4
CWM030 Rules disappearing in OL 2016? ( Yes, I searched before posting) Using Outlook 7
R When rules lose track of "specified folder" Using Outlook 6
I Outlook 2013 Rules - (client only) Using Outlook.com accounts in Outlook 1
B When I add more search strings to RULES, it is not processing them Using Outlook 3
A rules (flags) not working Using Outlook 7
K Lost Rules. Assistant accesses my mailbox. Disappeared Using Outlook 0
soadfan Outlook rules look up display name only Using Outlook 4
S Send email via SMTP - use transport rules to add to senders inbox (then rule to move to sent items Exchange Server Administration 1
Justo Horrillo Issue with rules in Outlook 2010 Using Outlook 4
J Outlook Rules - Changing auto-submit address in multiple rules, according to rule name Outlook VBA and Custom Forms 0
R When rules are working but invisible and unmanageable Using Outlook 2
P Transport Agent or Rules/Connectors Exchange Server Administration 1

Similar threads

Back
Top