Using Outlook.AdvancedSearch Method

Hi Scripting Guy,

I have been using your blog as reference and working with PowerShell for a while now. I have developed several scripts that handle running external .exe command programs that generate reports. Then, with PowerShell attach those reports to an email (in Outlook), add an HTML body with inline images, generate another report (file) with Excel, also through PowerShell, attach it as well and send it.

Now, I am dealing with scanning the Outlook Inbox for replies to those emails, and it feels like using the Outlook.AdvvanceSearch Method will be a good choice. I have found some samples on the web, but they are all for C# or VBScript. As a matter of fact I used a VBScript as a starting point and I have the following:

Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
        
$ol = New-Object -comObject Outlook.Application 

$strF = "subject:2015"

$strS = "Inbox" # search Inbox and all subfolders

$g_InboxSch = $Null

#VBScript sample I used as reference
#g_InboxSch = g_olApp.AdvancedSearch(Scope:=strS, Filter:=strF, SearchSubFolders:=True, Tag:="Complete")

$g_InboxSch = $ol.AdvancedSearch($strS, $strF, $true, "Complete");

However, I get the following error:

Exception calling "AdvancedSearch" with "4" argument(s): "The operation failed."
At line:1 char:1
+ $g_InboxSch = $ol.AdvancedSearch($strS, $strF, 1, "Complete");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : COMException

I have tried changing the $strS variable to something else, with no luck. I have tried the FolderPath for the actual folder as shown from the output of:

$ol.Session.Folders
Application            : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                  : 2
Session                : System.__ComObject
Parent                 : System.__ComObject
DefaultItemType        : 0
DefaultMessageClass    : IPM.Note
Description            : 
EntryID                : #deleted
Folders                : System.__ComObject
Items                  : System.__ComObject
Name                   : myemail@provider.com
StoreID                : #deleted
UnReadItemCount        : 0
UserPermissions        : System.__ComObject
WebViewOn              : True
WebViewURL             : #res://C:\Program Files\... edited
WebViewAllowNavigation : True
AddressBookName        : 
ShowAsOutlookAB        : False
FolderPath             : \\myemail@provider.com
InAppFolderSyncObject  : False
CurrentView            : System.__ComObject
CustomViewsOnly        : False
Views                  : System.__ComObject
MAPIOBJECT             : System.__ComObject
FullFolderPath         : \\myemail@provider.com
IsSharePointFolder     : False
ShowItemCount          : 1
Store                  : System.__ComObject
PropertyAccessor       : System.__ComObject
UserDefinedProperties  : System.__ComObject

Application            : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                  : 2
Session                : System.__ComObject
Parent                 : System.__ComObject
DefaultItemType        : 6
DefaultMessageClass    : IPM.Post
Description            : 
EntryID                : 000000001A... #edited
Folders                : System.__ComObject
Items                  : System.__ComObject
Name                   : Public Folders - AaronCalderon@ibc.com
StoreID                : 0000000... #edited
UnReadItemCount        : 0
UserPermissions        : 
WebViewOn              : False
WebViewURL             : 
WebViewAllowNavigation : True
AddressBookName        : 
ShowAsOutlookAB        : False
FolderPath             : \\Public Folders - myemail@provider.com
InAppFolderSyncObject  : False
CurrentView            : System.__ComObject
CustomViewsOnly        : False
Views                  : System.__ComObject
MAPIOBJECT             : System.__ComObject
FullFolderPath         : \\Public Folders - myemail@provider.com
IsSharePointFolder     : False
ShowItemCount          : 0
Store                  : System.__ComObject
PropertyAccessor       : System.__ComObject
UserDefinedProperties  : System.__ComObject

Application            : Microsoft.Office.Interop.Outlook.ApplicationClass
Class                  : 2
Session                : System.__ComObject
Parent                 : System.__ComObject
DefaultItemType        : 0
DefaultMessageClass    : IPM.Note
Description            : 
EntryID                : 000000005... #edited
Folders                : System.__ComObject
Items                  : System.__ComObject
Name                   : IBC Payroll Dept
StoreID                : 00000000... #edited
UnReadItemCount        : 0
UserPermissions        : System.__ComObject
WebViewOn              : False
WebViewURL             : 
WebViewAllowNavigation : True
AddressBookName        : 
ShowAsOutlookAB        : False
FolderPath             : \\Company Dept 1
InAppFolderSyncObject  : False
CurrentView            : System.__ComObject
CustomViewsOnly        : False
Views                  : System.__ComObject
MAPIOBJECT             : System.__ComObject
FullFolderPath         : \\Company Dept 1
IsSharePointFolder     : False
ShowItemCount          : 1
Store                  : System.__ComObject
PropertyAccessor       : System.__ComObject
UserDefinedProperties  : System.__ComObject

I wonder if you all have an insight on how to use this Method in PowersShell.

Thanks in advance for your time.




July 10th, 2015 11:06am

Simple:

($ol.AdvancedSearch("Inbox","urn:schemas:httpmail:subject='Re: proposed hud for closing'",$true,'tag1')).Results

This will find that exact subject.

You have to use schema defined search objects to get a result.

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 1:37pm

You may have to do it like this:

$s=$ol.AdvancedSearch("Inbox","urn:schemas:httpmail:subject='Re: proposed hud for closing'",$true,'tag1')
$s.Results

The previous only works on the first attempt.

July 10th, 2015 1:41pm

I was just reading about the DASL search filter* on the web and found another example (this time for Visual Studio**) that illustrates how to use such filter.

Your example did work, I tried to look for email with the subject "Lunch" and presto.

Now, I just need to find out more info about the schema defined search object.

Back to searching the web.

Foot Notes:

*DASL Search filter
https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook._application.advancedsearch(v=office.14).aspx
**Visual Studio Example
http://blogs.msdn.com/b/philliphoff/archive/2008/02/18/query-your-outlook-inbox-with-linq-to-dasl.aspx 
Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 2:20pm

Just to clarify the purpose of my script. I am actually trying to look for emails on the inbox that is on the "\\Company Dept 1" folder, so in the Scope parameter I entered the following:

"'\\Company Dept 1\Inbox'" 

Please note the dual quotation, single quotes enclosed with double quotes. This is following the DASL Search filter as explained on MSDN.Microsoft.com:

https://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.outlook._application.advancedsearch(v=office.14).aspx

With this minor change I get results from emails that are inside the intended inbox.

Thanks. 

July 10th, 2015 2:33pm

Found some good information regarding the schema defined search

http://blogs.msdn.com/b/andrewdelin/archive/2005/05/11/416312.aspx
http://blogs.msdn.com/b/andrewdelin/archive/2005/08/08/448882.aspx
Free Windows Admin Tool Kit Click here and download it now
July 10th, 2015 2:48pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics