【问题标题】:Downloading .xlsx attachment from Outlook of Specific Date using Powershell使用 Powershell 从特定日期的 Outlook 下载 .xlsx 附件
【发布时间】:2021-01-01 21:53:47
【问题描述】:

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

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 附件。但是在此之后,我不知道如何保存/下载它们。

【问题讨论】:

  • 请将您的代码粘贴到您的问题中,而不是像截图一样添加它。如果有人想使用代码进行编辑,它需要是文本而不是图片。插入问题后,标记代码并按 CTRL+K 以将其格式化为代码。
  • 在你的for循环中你引用了$email,但是在你使用$mail的上一行?也许这就是问题所在?请 - 正如@Adis1102 所说 - 将您的代码粘贴到问题中。
  • 您好,代码已更新。请仔细查看
  • 那么你当前脚本的输出是什么?
  • 您不能在空值表达式上调用方法。在 line:17 char:5 + $_.saveasfile((Join-Path $FilePath "$out")) }}} + ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

标签: powershell scripting powershell-4.0


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-09-14
    • 2016-06-29
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2022-01-15
    相关资源
    最近更新 更多