2017-02-14 11 views
1

senderemailaddressというワイルドカードに一致する添付ファイルをOutlookから抽出しようとしています。以下のコードでわかるように、私は2つのフィルターを試していましたが、役に立たなかった。ワイルドカードを使用して特定の送信者からの添付ファイルをOutlookから読み取る

コード内で現在アクティブな非コメントフィルタを使用すると、コードはエラーをスローしたり、テストケースに一致する添付ファイルをダウンロードしたりしません。しかし、コメント付きフィルタを有効にして実行すると、次のエラーが発生します。

Exception calling "Restrict" with "1" argument(s): "Cannot parse condition. Error at 
"like"." 
At C:\Users\acer\Desktop\outlook.ps1:42 char:2 
+ $filteredItems = $folder.items.Restrict($filter) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ComMethodTargetInvocation

コード:

$filepath = "C:\folder\subfolder\subsubfolder\" 
function downloadFiles { 
    $filter = "[UnRead]=true AND [SenderEmailAddress] -match @example" 
    #$filter = "[UnRead]=true AND [SenderEmailAddress] -like '*@example*'" 

    Add-Type -Assembly "Microsoft.Office.Interop.Outlook" | Out-Null 
    $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type] 

    $outlook = New-Object -ComObject Outlook.Application 
    $namespace = $outlook.GetNameSpace("MAPI") 

    $folder = $namespace.GetDefaultFolder($olFolders::olFolderInBox) 

    #$folder.Items | select SenderEmailAddress 

    $filteredItems = $folder.Items.Restrict($filter) 

    foreach ($objMessage in $filteredItems) { 
     $intCount = $objMessage.Attachments.Count 

     if ($intCount -gt 0) { 
      for ($i=1; $i -le $intCount; $i++) { 
       $objMessage.Attachments.Item($i).SaveAsFile($filepath+$objMessage.Attachments.Item($i).FileName) 
      } 
     } 
     $objMessage.Unread = $false 
    } 

    $outlook.Close 
} 

downloadFiles 

EDIT1:提案のおかげでみんな。 私はそれをunread = trueでフィルタリングし、フィルタリングされたメールのプロパティからsenderemailaddressと一致するパターンを実行することができました。変更されたコードの追加

$filepath = "C:\folder\subfolder\subsubfolder\" 
    function downloadFiles { 
     $filter="[UnRead]=true" 
    $emailfilter = "*@xyz.co.in" 
    $subjectfilter = "test file*" 

    Add-Type -Assembly "Microsoft.Office.Interop.Outlook" | Out-Null 
     $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type] 

     $outlook = New-Object -ComObject Outlook.Application 
     $namespace = $outlook.GetNameSpace("MAPI") 

     $folder = $namespace.GetDefaultFolder($olFolders::olFolderInBox) 

     #$folder.Items | select SenderEmailAddress 

     $filteredItems = $folder.Items.Restrict($filter) 

     foreach ($objMessage in $filteredItems) { 
      $subject = $objMessage.Subject 
      $emailaddress = $objMessage.SenderEmailAddress 

      if(($emailaddress -like $emailfilter) -and ($subject -like $subjectfilter)){ 
      $intCount = $objMessage.Attachments.Count 

      if ($intCount -gt 0) { 
       for ($i=1; $i -le $intCount; $i++) { 
        $objMessage.Attachments.Item($i).SaveAsFile($filepath+$objMessage.Attachments.Item($i).FileName) 
       } 
      } 
      $objMessage.Unread = $false 
      } 

      else {continue} 
     } 
     $outlook.Close 
    } 

    downloadFiles 

は今、問題は、このスクリプトをスケジュールされましたか?コマンドプロンプトでpowershellパスを使ってこのスクリプトを実行するとうまくいきます。しかし、私が同じスケジュールを立てると、それは完了していません。 TaskManagerのタスクスケジューラによって生成されたOutlookプロセスを見ることができ、プロセスを手動で終了して同じプロセスを終了する必要があります。何か案は?

答えて

0

EWSを使用します。 Outlookにプログラムでアクセスできるようにする必要がありません。

最も簡単な方法は、ナゲットからダウンロードすることです。あなたが最初にダウンロードnugetによってPowerShellでこれを行うことができます:

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" 
$targetNugetExe = "D:\Program Files\nuget\nuget.exe" # chaneg path to suit 
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe 
Set-Alias nuget $targetNugetExe -Scope Global -Verbose 

次にEWSのnugetパッケージをダウンロード:

Set-Location D:\Temp # change to suit 
nuget install 'Microsoft.Exchange.WebServices' 

を今、あなたは:)

# load the assembly 
[void][Reflection.Assembly]::LoadFile("D:\Temp\Microsoft.Exchange.WebServices.2.2\lib\40\Microsoft.Exchange.WebServices.dll") 

# set ref to exchange - may need to change the version 
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) 

# replace with your email address 
$email = "[email protected]" 

# grab your own credentials 
$s.UseDefaultCredentials = $true 

# discover the url from your email address 
$s.AutodiscoverUrl($email) 

# get a handle to the inbox 
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 

#create a property set (to let us access the body & other details not available from the FindItems call) 
$psPropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) 
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text 

$items = $inbox.FindItems(100) # change to suit 

# loop through the emails - at this point, we don't have full info - we have to Load the email 
# restrict on what we do know - if the email is read and if it has attachments 
$items | where { !$_.IsRead -and $_.HasAttachments } | ForEach-Object { 
    # load the email, so we can get to other properties, like attachments, sender, etc 
    $_.Load() 
    # does the sender match our wildcard? 
    if ($_.Sender -like '*lmnopqr*') { 
    # loop through all file attachments 
    $_.Attachments | Where-Object { $_ -is [Microsoft.Exchange.WebServices.Data.FileAttachment] } | ForEach-Object { 
     # save them (yes, Load = Save in this instance!) 
     $_.Load("D:\Temp\$($_.Name)") 
    } 
    } 
} 

を使用して始めることができるためEWS linkを参照してください。 EWSとやりとりする方法の詳細。

また、私の他のSO postを参照してください。これは、EWSアセンブリの入手先が古くなっていますが、余分なEWSメソッド/プロパティに関する有用な情報があります。独自のものを使用していない場合(またはPowerShellを実行しているプロセスにExchangeアカウントがない場合)、代替資格情報の使用方法の詳細も表示されます。

+0

は私がすることができません行うには、フィルタ-contains lmnopqrを使用することができ オブジェクトのプロパティのリストに表示されているにもかかわらず、スクリプト内のisreadプロパティがアクセスされます。 Antの提案 –

+0

$ items | select *はアイテムの一部としてisreadプロパティを表示します –

+0

'$ items [0] .IsRead'は何を返しますか? – TechSpud

0

プロバイダは、この方法のフィルタにLikeを使用することを許可していません。このからMSDN article

「含む」操作を実行する方法はありません。たとえば、 は、[件名]フィールドに特定の 語を持つ項目を検索するために[検索]または[制限する]を使用できません。代わりに、AdvancedSearch メソッドを使用するか、フォルダ内のすべての項目をループして、 InStr関数を使用してフィールド内で検索を実行できます。

+0

しかし、文字列が数字の場合はcontainsを使用することができました。 例:私のsendermailaddressだった場合は、この/ O = XXXXX/OU = Exchange管理グループ (FYDIBOHF23SPDLT)/ CNは、受信者を=/CN = 02DE69998DE7457BBC1CAA86E82818DA-lmnopqr私は同じ –

関連する問題