简体   繁体   English

使用 Powershell 从特定日期的 Outlook 下载 .xlsx 附件

[英]Downloading .xlsx attachment from Outlook of Specific Date using Powershell

I have the below script.我有以下脚本。 This $Tests shows the list of .xlsx attachment of specific date but is not able to download and throws an error.此 $Tests 显示特定日期的 .xlsx 附件列表,但无法下载并引发错误。 Please find the below script.请找到以下脚本。

Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder = $inbox.Folders | Where-Object {$_.Name -eq “Test”}
$mail=$subfolder.Items |Select-Object -Property "ReceivedTime",@{name="Attachments";expression={$_.Attachments|%{$_.DisplayName}}} | Where-Object{$_.attachments -match ".xlsx" -and ($_.receivedtime -match "9/15/2020")} | Select-Object "attachments"
$Test = $mail.attachments
foreach ($out in $test) {$_.attachments|foreach {
Write-Host $_.filename
$Filename = $_.filename
If ($out.Contains("xlsx")) {
$_.saveasfile((Join-Path $FilePath "$out")) }}}

I am able to filter the .xlsx Attachments with Specific Date.我能够过滤具有特定日期的 .xlsx 附件。 But after this, I don't know how to save/download them.但在此之后,我不知道如何保存/下载它们。

Working with com objects can be rather frustrating in powershell.在 powershell 中使用 com 对象可能会令人沮丧。 I recommend you get extremely familiar with Get-Member .我建议您非常熟悉Get-Member You really have to interrogate each object.您真的必须询问每个对象。 I've simplified your script as well as tested thoroughly.我已经简化了您的脚本并进行了彻底的测试。 It will download each matching attachment (name) from each match email (received date)它将从每个匹配的电子邮件(接收日期)中下载每个匹配的附件(名称)

Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder.Items | Where-Object {$_.receivedtime -match "9/20/2020" -and $($_.attachments).filename -match '.xlsx'} | foreach {
    $filename = $($_.attachments | where filename -match '.xlsx').filename
    foreach($file in $filename)
    {
        Write-Host Downloading $file to $filepath -ForegroundColor green
        $outpath = join-path $filepath $file
        $($_.attachments).saveasfile($outpath)
    }
}

You may use this for more of an "in-line" approach.您可以将其用于更多“内联”方法。

Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olDefaultFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = New-Object -comobject Outlook.Application
$mapi = $outlook.GetNameSpace(“MAPI”)
$inbox = $mapi.GetDefaultFolder(6)
$FilePath= "c:\temp\Test\"
$subfolder.Items | Where-Object {$_.receivedtime -match "9/20/2020" -and $($_.attachments).filename -match '.xlsx'} | foreach {
    foreach($attachment in $($_.attachments | where filename -match '.xlsx'))
    {
        Write-Host Downloading $attachment.filename to $filepath -ForegroundColor green
        $attachment.SaveAsFile((join-path $FilePath $attachment.filename))
        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM