I have a powershell script to get e-mails from an Outlook Inbox subfolder, but it only returns e-mails after three or more executions. Is there a way to prompt Outlook to refresh the Inbox subfolder before the script prompts the scan?
I am accessing the mailbox through another machine to guarantee the emails are on the subfolder and the flags are cleared.
Here it is a sample of the script:
# Add Interop Assembly
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
# Create outlook COM object to search folders
$Outlook = New-Object -ComObject Outlook.Application
$OutlookNS = $Outlook.GetNamespace("MAPI")
$OutlookNS.Logon($null, $null, $false, $false);
$OutlookNS.SendAndReceive($false) | Wait-Job
Write-Output "Getting an email from Outlook..."
$Email = $OutlookNS.Folders.Item($Account).Folders.Item($Folder).Items | ? { ($_.FlagStatus -match 0) } | Sort ReceivedTime | Select -First 1
if($Email){
Write-Output $Email.Subject
} else {
Write-Output "No data"
}
...