首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Powershell从指定日期的Outlook下载.xlsx附件

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

Stack Overflow用户
提问于 2020-09-15 15:10:11
回答 1查看 389关注 0票数 0

我有下面的脚本。此$Tests显示特定日期的.xlsx附件列表,但无法下载并抛出错误。请找到下面的脚本。

代码语言:javascript
复制
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")) }}}

我能够过滤带有特定日期的.xlsx附件。但在此之后,我不知道如何保存/下载它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-20 16:46:45

在powershell中使用com对象可能相当令人沮丧。我建议您非常熟悉Get-Member。你真的需要审问每一个对象。我已经简化了你的脚本,并进行了彻底的测试。它将从每个匹配电子邮件(接收日期)下载每个匹配的附件(名称)

代码语言:javascript
复制
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)
    }
}

您可以将其用于更多的“内联”方法。

代码语言:javascript
复制
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))
        
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63896788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档