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プロセスを見ることができ、プロセスを手動で終了して同じプロセスを終了する必要があります。何か案は?
は私がすることができません行うには、フィルタ-contains lmnopqrを使用することができ オブジェクトのプロパティのリストに表示されているにもかかわらず、スクリプト内のisreadプロパティがアクセスされます。 Antの提案 –
$ items | select *はアイテムの一部としてisreadプロパティを表示します –
'$ items [0] .IsRead'は何を返しますか? – TechSpud