在 PowerShell 中保存附件并从共享邮箱移动电子邮件

Save attachments and move e-mails from shared box in PowerShell

我有一个关于 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.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.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)
    }
}

你能帮我完成这个吗?

非常感谢!!

永远不要遍历文件夹中的所有项目 - 这就像在 SQL 中有一个 SELECT 子句但没有 WHERE 子句。使用 Items.RestrictItems.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_NAME MAPI 属性.

其次,我不是 PS 专家,但您的语法不正确 - $fldr.Items.ReceivedTime 无效:ReceivedTime 是 [=17= 上的 属性 ] 对象(从 Items 集合返回),而不是 Items 集合本身。

这按预期工作。它不性感,但它有效:)

#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)
    }