我有一個關于 Powershell 的問題。我想要做的是遍歷共享(組)郵箱的收件箱,如果電子郵件來自一組 2 個發件人姓名,則將 CSV 附件保存到特定檔案夾,然后將電子郵件移動到收件箱的另一個子檔案夾。我今天早上設法使它適用于一個發件人姓名的回圈保存附件部分,但后來不知何故破壞了代碼(在第一個 foreach 回圈上)。另外,我無法運行“移動電子郵件”部分。
這是我得到的代碼:
#define outlook object and ask to pick folder
$outlook = New-Object -comobject outlook.application
$mapi = $outlook.GetNamespace("MAPI")
$fldr = $mapi.PickFolder()
$DestFolder = "\\MAIBOXNAME\Inbox\01.Reports"
#use i as counter to define filenumber to save PendingOrders
$i = 1
$fldr.Items| foreach {$fldr.Items.Sender.Name -like "Name1" - or $fldr.Items.Sender.Name -like "Name2"
$year = $fldr.Items.ReceivedTime.ToString("yyyy")
$month = $fldr.Items.ReceivedTime.ToString("MM")
$fdate = $fldr.Items.ReceivedTime.ToString("yyyy.MM.dd")
$fpath = "Path\02.Daily Files\" $year '\' $year '.' $month '\' $fdate
#loop through attachments to find CSV extension - if found, save file to target path with the counter in the name and update the counter
$fldr.Items.Attachments|foreach {
$_.Filename
If ($_.filename.Contains("CSV")) {
$fname = $_.filename
$_.saveasfile((Join-Path $fpath "00$i $fname"))
$i = $i 1
}
$_.Move($DestFolder)
}
}
你能幫我完成這個嗎?
非常感謝!!
uj5u.com熱心網友回復:
永遠不要遍歷檔案夾中的所有專案——這就像在 SQL 中有一個沒有 WHERE 子句的 SELECT 子句。使用Items.Restrictor Items.Find/FindNext進行查詢@SQL=("http://schemas.microsoft.com/mapi/proptag/0x0C1A001F" LIKE '%Name1%') OR ("http://schemas.microsoft.com/mapi/proptag/0x0C1A001F" LIKE '%Name1%')(DASL 名稱用于PR_SENDER_NAMEMAPI 屬性。
其次,我不是 PS 專家,但您的語法已關閉 -$fldr.Items.ReceivedTime無效:ReceivedTime是物件上的MailItem屬性(從 Items 集合回傳),而不是Items集合本身。
uj5u.com熱心網友回復:
這按預期作業。它不性感,但它有效:)
#define outlook object and ask to pick folder
$outlook = New-Object -comobject outlook.application
$mapi = $outlook.GetNamespace("MAPI")
$fldr = $mapi.Stores['xxxxxxxxxx'].GetRootFolder().Folders("Inbox")
$DestFolder = $mapi.Stores['xxxxxxxxxx'].GetRootFolder().Folders("Inbox").Folders("01.Reports")
$delFile = 1
$fldr.Items|
Where-Object{$_.SenderName -eq "Name1" -or $_.SenderName -eq "Name2"} |
ForEach-Object{
$year = $_.ReceivedTime.ToString("yyyy")
$month = $_.ReceivedTime.ToString("MM")
$fdate = $_.ReceivedTime.ToString("yyyy.MM.dd")
#Find newest csv file in folder and use it as counter, if empty start from 1
$fpath = "SomePath\" $year '\' $year '.' $month '\' $fdate
#create temporary csv file to start archiving xls file from 1
New-Item -Path (Join-Path $fpath "00.csv") -ItemType File
if ($delFile -ne 1) {
Remove-Item (Join-Path $fpath "00.csv")
}
$delFile = $delFile 1
$filter="*CSV"
$lastCsv= Get-ChildItem -Path $fpath -Filter $filter | Sort-Object LastAccessTime -Descending | Select-Object -First 1
if ($lastCsv) {
$i = [int]$lastCsv.name.substring(0,3) 1
}
else {
$i = [int]1
}
#loop through attachments to find CSV extension - if found, save file to target path with the counter in the name and update the counter
$_.Attachments|foreach {
$_.FileName
If ($_.FileName.Contains("CSV")) {
$fname = $_.FileName
$i = '{0:d3}' -f $i
$_.SaveAsFile((Join-Path $fpath "$i $fname"))
$i = [int]$i 1
}
}
$_.Move($DestFolder)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/461064.html
上一篇:PHP發送郵件問題
