- #FindOldReports and Archive Them
- $today = get-date -format dMMyy
- Get-ChildItem '\\servername\TaskSchedulerScripts' *.csv -Recurse -Exclude "this.csv", "that.CSV" |
- ForEach-Object {Move-Item $_.FullName -Destination "\\servername\TaskSchedulerScripts\Archive\$($Today)_$($_.Name)"}
- #Open Outlook and find emails from today
- $ol = New-Object -ComObject Outlook.Application
- $ns = $ol.GetNamespace('mapi')
- $mb = $ns.Stores['some@email.com'].GetRootFolder()
- $inbox = $mb.Folders['Reports']
- $DailyReports = $inbox.Items | Where-Object {$_.ReceivedTime -match (get-date -Format d)}
- #Define Attachments Path
- $filepath = '\\servername\TaskSchedulerScripts'
- #Save Attachments
- $DailyReports.attachments | ForEach-Object {$_.saveasfile((Join-Path $filepath $_.FileName))}
- #Mark items as read
- $inbox.items | ForEach {$_.UnRead = $false}
- sleep -Seconds 15
- #Quit Outlook and Release COM object
- $ol.Quit()
- [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Ol)
- Get-Process -Name OUTLOOK | Stop-Process -Force
Outlook COM Mark email as read
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Outlook COM Mark email as read
Hello, I'm having issues marking emails as read when ran as a PS1 file. Everything works expect marking them as read.
Re: Outlook COM Mark email as read
You should probably ask specific questions about external subsystems in teh developers forum for the subsystem.
Specifically, I see that you are not setting the mail item as beinf "Read". You must set that directly through the COM proxy.
Here is a link to the MailItem Unread property: https://docs.microsoft.com/en-us/office ... tem.unread
Specifically, I see that you are not setting the mail item as beinf "Read". You must set that directly through the COM proxy.
Here is a link to the MailItem Unread property: https://docs.microsoft.com/en-us/office ... tem.unread
Re: Outlook COM Mark email as read
Hey Jim, I forgot to include that it does work if I run it one line at a time
It’s only when it’s all executed at the same time, in a PS1 or in ISE
It’s only when it’s all executed at the same time, in a PS1 or in ISE
Last edited by localpct on Fri Aug 20, 2021 5:55 am, edited 1 time in total.
Re: Outlook COM Mark email as read
Ok I see you mistake. I couldn't see the code because it was colorized incorrectly.
When you filter and then iterate a collection and change the filter component then the filter skips items in the collection because you are changing a dynamic collection.
Get an array of Mail IDs and then enumerate that and you won't be fighting a changing dynamic collection.
When you filter and then iterate a collection and change the filter component then the filter skips items in the collection because you are changing a dynamic collection.
Get an array of Mail IDs and then enumerate that and you won't be fighting a changing dynamic collection.
Re: Outlook COM Mark email as read
Hey I edited my second post after you posted but if I run it one line at a time, it works
If I run in PS1 or select all then run in ISE, it fails to mark emails as read
If I run in PS1 or select all then run in ISE, it fails to mark emails as read
Re: Outlook COM Mark email as read
Discovered I needed to add in a start-sleep for some reason our exchange couldn't handle it lol
Re: Outlook COM Mark email as read
Unfortunately that is not possible as the updates occur differntly than you think. IN enumerted "Finds" the set of objects is changed betwen each iteration which causes the index to constantly be off. Enumerating bottom to top works if the search is not sorted. If sorted then retrive the mail ids and alter the emails using the index array which will not change due to alterations. What happens on Exchenge has nothing to do with this. If Exchange can't process the incremental changes an exception will be thrown.
Re: Outlook COM Mark email as read
AFter re reading and thinking about this. I do understand what you're saying so I moved the script to something like this
I do honestly feel something is wrong with the object for a few reasons, there aren't many success stories on the web and If I attempt to delete/read the file within the loop or as a whole ($Folder.Items | ForEach-Object { $_.Delete()} )it wont process the files and I'll see an item removed from the folder
- $Ol = New-Object -comobject Outlook.Application
- $namespace = $Ol.GetNameSpace("MAPI")
- $Folder = $namespace.Folders.Item('some@email.com').Folders.Item('REPORTS')
- $Folder.Items | ForEach-Object {
- $files = $_.attachments
- foreach($f in $files)
- {
- $f.saveasfile((Join-Path 'C:\Data' $f.filename ))
- }
- }
- #Quit Outlook and Release COM object
- $ol.Quit()
- [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Ol)
- Get-Process -Name OUTLOOK | Stop-Process -ErrorAction SilentlyContinue -Force