您需要自己完成大部分工作,但这是来自我的一个类似脚本的代码,我已经对其进行了分解,以使其更具可读性,希望您能够开始。
#Params
$Account = "Mailbox.Searchme@contoso.com"
$Folder = "Inbox"
$SubjMatch = "Reports"
#Create outlook COM object to search folders
$Outlook = New-Object -ComObject Outlook.Application
$OutlookNS = $Outlook.GetNamespace("MAPI")
#Get all emails from specific account and folder
$AllEmails = $OutlookNS.Folders.Item($Account).Folders.Item($Folder).Items
#Filter to emails with attatchments and specific subject line (-match uses RegEx)
$ReportsEmails = $AllEmails | ? { ($_.Subject -match $SubjMatch) -and ($_.Attachements.Count -gt 0) }
#Grab the most recently recieved email
$LatestReportEmail = $ReportsEmails | Sort ReceivedTime | Select -Last 1
#Get the xlsx file(s) and save them
$LatestReportEmail.Attachments | ? {$_.FileName -match "\.xlsx$"} | % {
$_.SaveAsFile("C:\path\to\$($_.FileName)")
}
#Quit Outlook COM Object
$Outlook.Quit()
在尝试运行此操作之前,您应该关闭Outlook,而且在大文件夹上运行此操作可能会非常慢(由于某些原因,主要是筛选部分),祝您好运。