EWS Managed API Demo: SearchFilterCollection

Hallo zusammen,

Wie SerachFilter funktionieren habe ich euch ja schon im vorhergehenden Artikel erklärt. Was wenn man aber nun mehr als nur ein Suchkriterium anwenden möchte? Dann nimmt man eine SearchFilterCollection.

Der relevante Code dazu ist untenstehend:

$SearchFilter1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Request")
$SearchFilter2 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Pos")
$SearchFilter3 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Tent")
$SearchFilter4 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Neg")


$MySearchFilterCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::Or)
$MySearchFilterCollection.Add($SearchFilter1)
$MySearchFilterCollection.Add($SearchFilter2)
$MySearchFilterCollection.Add($SearchFilter3)
$MySearchFilterCollection.Add($SearchFilter4)
           
                            
$View = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) 
$ExResult = $EWService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MySearchFilterCollection,$View)

Und so sieht dann das ganze Script aus - kann ebenfalls heruntergeladen werden.

###############################################################################
# EWS SearchfilterCollection in Inbox
# EWS Managed API DEMO
# Version 1.0 / 15.07.2012
# Andres Bohren / www.icewolf.ch / blog.icewolf.ch / info@icewolf.ch
###############################################################################


###############################################################################
# Function WriteLog
###############################################################################
Function WriteLog {
 PARAM (
 [string]$pLogtext
 )
 
    $pDate =  $(get-date -format "dd.MM.yyyy HH:mm:ss")
      
    $sw = new-object system.IO.StreamWriter($LogPath, 1)
    $sw.writeline($pDate + " " + $pLogtext)
    $sw.close()


}

###############################################################################
# Function SearchInbox
# Searches in Inbox by applying a SearchFilter
###############################################################################

Function SearchInbox
{
 
    Try {
             
            $SearchFilter1 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Request")
            $SearchFilter2 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Pos")
            $SearchFilter3 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Tent")
            $SearchFilter4 = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, "IPM.Schedule.Meeting.Resp.Neg")


            $MySearchFilterCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::Or)
            $MySearchFilterCollection.Add($SearchFilter1)
            $MySearchFilterCollection.Add($SearchFilter2)
            $MySearchFilterCollection.Add($SearchFilter3)
            $MySearchFilterCollection.Add($SearchFilter4)
          
                            
            $View = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000) 
            $ExResult = $EWService.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MySearchFilterCollection,$View)
           
           
            If ($ExResult -ne $Null)           
                {
                Foreach ($Item In $ExResult.Items )
                    {
                        Write-Host ("ITEM Found: subject=" + $Item.Subject + " ItemClass=" +$Item.ItemClass + " LastModifiedTime=" + $Item.LastModifiedTime)
                        WriteLog ("ITEM Found: subject=" + $Item.Subject + " ItemClass=" +$Item.ItemClass + " LastModifiedTime=" + $Item.LastModifiedTime)                       
                    }
                }

     
    } catch [system.exception] {
        WriteLog ("Error in DeleteMeetingRequests: " + $_.Exception.ToString())
        Write-Host ("Error in DeleteMeetingRequests: " + $_.Exception.ToString())       
    }

###############################################################################
# MainProgramm
###############################################################################

# Global Vars
[string]$EwsApiDll = "C:\Program Files\Microsoft\Exchange\Web Services\1.2\Microsoft.Exchange.WebServices.dll"
[string]$LogPath = "T:\Visual Basic\PowerShell\EWSScript\EWSSearchFilterCollection.log"

[string]$Email = "ewservice@icewolf.ch"
[string]$Username = "ewservice"
[string]$Password = "MySecretPassword!"
[string]$Domain = "Corp"
[string]$EWSURL = ""
#[string]$EWSURL = "https://icesrv01/EWS/Exchange.asmx"
#[string]$EWSURL = "https://icesrv01.corp.icewolf.ch/EWS/Exchange.asmx"
[string]$ImpersonationMailbox = "a.bohren@icewolf.ch"


# Main Programm

Try {
        WriteLog "###############################################################################"
        WriteLog "### Starting Script"
        WriteLog "###############################################################################"

        # Import EWS Managed API DLL
        Import-Module -Name $EwsApiDll
        Write-Host ("Imported EWS Module")
        WriteLog ("Imported EWS Module")

        # Connect to EWS
        #$EWService = ConnectEWS ($Email, $Username, $Password, $Domain, $EWSURL)
        # Create a new Exchange Service Object
        $EWService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
        $EWService.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials($Username,$Password,$Domain)

        Write-Host ("Connecting to EWS...")
        WriteLog ("Connecting to EWS...")

        If ($EWSURL -eq "")
            {
                $EWService.AutodiscoverUrl($Email)
                Write-Host ("Using Autodiscover")
            } else {
                $EWService.Url = $EWSURL
                Write-Host ("Using EWS URL")
            }


            $Mailbox = $ImpersonationMailbox
            Write-Host ("-->Impersonation to Mailbox: " + $Mailbox)
            Writelog ("-->Impersonation to Mailbox: " + $Mailbox)
               
            $EWService.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $Mailbox)
           
            # Search in Inbox
            SearchInbox
                 

        # Clean Up
        WriteLog "###############################################################################"
        WriteLog "### Finished"
        WriteLog "###############################################################################"
        Write-Host "Finished"
       
 

    } catch [system.exception] {
        WriteLog ("Error in EmptyRecycleBin " + $_.Exception.ToString())
        Write-Host ("Error in EmptyRecycleBin " + $_.Exception.ToString())       
}

Und so sieht das Script dann in Powershell aus - man sieht, dass die verschiedenen MessageClasses gefunden worden sind.

Grüsse
Andres Bohren