Outlook PowerShell script does not retrieve latest email -


I have a script that reads an Outlook folder for e-mail, selecting the most recently received email , And saves its attachment in a directory.

It does not work properly - It seems that it only knows what email I receive, if I open Outlook before running the script - otherwise, it thinks that the most recent The email received is only that

Is there any way to refresh Outlook to refresh before scanning the script?

My code is below:

$ filepath = $ args [0] $ account = $ args [1 ] & Lt; ## File Path $ filepath = "I: \ folder" $ account = "Account@host.com" ## #set approach $ o = to open new object-conebiz Outlook. App $ n = $ o.GetNamespace ("MAPI") $ account = $ n. Folter | ? {$ _. Name-EC $ account}; $ Inbox = $ account.Folders | ? {$ _. Name-mail 'inbox'}; $ Data = INBox.Folders | ? {$ _. Name-match 'data'}; $ F = $ Data.Folders | ? {$ _. Name-mail 'MyTargetFolder'}; $ Email = $ f.Items | Sort-object receive-time-desensing | Select-object-first 1 # Log the email we are looking for, and mention the attachment if they exist. Write-output "received at last email $ ($ email.receivedtime), attached file (s) are: (if any)" $ email.attachments | % {Write-output $ _ Filename} # If there is at least one attachment in the email, save them in the file path. If ($ email.attachments.count -gt 0) {$ email.attachments | The latest emails on% {$ _ Saveasfile (co-path $ filepath $ _. Filename))}} and {write-output "$ ($ email.receivedtime) have no attachments!" }

you think like

  $ A .store | ? {$ _. DisplayName -q $ account} | % {If ($ _. IsCachedExchange) {start-job} {$ n.SendAndReceive ($ false)} | Wait-Job}}  

To ensure that if it is in the cache mode it will send & amp; Receive before checking email, see Edit the note below.

You may also want to make sure that the user is not in offline mode with one:

  if ($ n.offline) {write "execute this script You must be online before "- then red, start-up 5; Brake}  

In this way, if the user is in offline mode then it will show red text, they need to be in online mode, wait 5 seconds, and then exit the script.

Edit: Thank you for the inkbar, wait for it to send to Outlook, before proceeding to make sure that you get the latest email Has been cached. Thanksgiving, I did not know about that command, it is very easy!


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -