Answered by:
PowerShell Outlook Advanced Search Complete Event

Question
-
Hello !
In a PowerShell script, I want to do some research in Windows local mails.
I have the following script :Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $outlook = New-Object -com Outlook.Application; $namespace = $outlook.GetNamespace("MAPI"); Function Get-OutlookInbox { $accountsList = $namespace.Folders $query = "Test" $filter = "urn:schemas:httpmail:subject LIKE '"+$query+"'" foreach($account in $accountsList) { write-host "SEARCHING IN MAILBOX : " $account.name $scope = $account.FolderPath $search = $outlook.AdvancedSearch("'$scope'", $filter, $True) Start-Sleep -Seconds 10 foreach ($result in $search.Results) { $result.Subject $result.ReceivedTime $result.SenderName } } $inbox = Get-OutlookInbox $inbox
It works well thanks to the `Start-Sleep -Seconds 10` as the `$outlook.AdvancedSearch` function is asynchronous, but I don't really like this way of developing, a bit crappy.
I would like to use AdvancedSearchComplete Event but I don't know how to use it in PowerShell, and the documentation is for VBA. I'm not very good at PowerShell development, I don't find a way to do it.
Thanks for your help !
- Edited by Quentin Birgaentzle Monday, February 11, 2019 10:07 AM
Monday, February 11, 2019 9:58 AM
Answers
-
See:
help Register-ObjectEvent -online
\_(ツ)_/
- Marked as answer by Quentin Birgaentzle Friday, February 22, 2019 8:38 AM
Monday, February 11, 2019 10:27 AM -
Hi,
Yes, thanks to Register-ObjectEvent, I found a solution.
If necessary, for other members, my updated script :Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $outlook = New-Object -com Outlook.Application; $namespace = $outlook.GetNamespace("MAPI"); Register-ObjectEvent -InputObject $outlook -EventName "AdvancedSearchComplete" -Action { Write-Host "ADVANCED SEARCH COMPLETE" $Args.Scope if ($Args.Results) { foreach ($result in $Args.Results) { write-host "==================================================" write-host $result.Subject write-host $result.ReceivedTime write-host $result.SenderName write-host "==================================================" } } } Function Get-OutlookInbox { $accountsList = $namespace.Folders $query = "Test" $filter = "urn:schemas:httpmail:subject LIKE '%"+$query+"%'" foreach($account in $accountsList) { $scope = $account.FolderPath $search = $outlook.AdvancedSearch("'$scope'", $filter, $True) } } Get-OutlookInbox
- Marked as answer by Quentin Birgaentzle Friday, February 22, 2019 8:38 AM
Friday, February 22, 2019 8:38 AM
All replies
-
See:
help Register-ObjectEvent -online
\_(ツ)_/
- Marked as answer by Quentin Birgaentzle Friday, February 22, 2019 8:38 AM
Monday, February 11, 2019 10:27 AM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.
Friday, February 22, 2019 5:52 AM -
Hi,
Yes, thanks to Register-ObjectEvent, I found a solution.
If necessary, for other members, my updated script :Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null $outlook = New-Object -com Outlook.Application; $namespace = $outlook.GetNamespace("MAPI"); Register-ObjectEvent -InputObject $outlook -EventName "AdvancedSearchComplete" -Action { Write-Host "ADVANCED SEARCH COMPLETE" $Args.Scope if ($Args.Results) { foreach ($result in $Args.Results) { write-host "==================================================" write-host $result.Subject write-host $result.ReceivedTime write-host $result.SenderName write-host "==================================================" } } } Function Get-OutlookInbox { $accountsList = $namespace.Folders $query = "Test" $filter = "urn:schemas:httpmail:subject LIKE '%"+$query+"%'" foreach($account in $accountsList) { $scope = $account.FolderPath $search = $outlook.AdvancedSearch("'$scope'", $filter, $True) } } Get-OutlookInbox
- Marked as answer by Quentin Birgaentzle Friday, February 22, 2019 8:38 AM
Friday, February 22, 2019 8:38 AM -
If necessary, for other members, my updated script :
Write-Host Considered Harmful .... ;-) :-D
Live long and prosper!
(79,108,97,102|%{[char]$_})-join''
Friday, February 22, 2019 8:41 AM -
Yes, I know ;), that's just a quick developped script for this topic, as my complete script is more complexFriday, February 22, 2019 8:45 AM
-
Write-Host is not harmful but it does create havoc with unsuspecting users. It is a good trace tool for simple tracing. "Write-Warning" is more useful and can be logged.
It is much more useful to create objects for returned data. When running in an event we cannot return objects but they can be assigned to a global to persist the created objects. The easiest way to do this is "$global:eventResults += $event". We can then process the results whenever we want or when we get a completion event.
\_(ツ)_/
Friday, February 22, 2019 8:55 AM