【问题标题】:How to fix corrupted files when download from outlook using powershell使用PowerShell从Outlook下载时如何修复损坏的文件
【发布时间】:2021-02-18 19:46:45
【问题描述】:

我有一个 powershell 脚本,它会自动从 Outlook 下载并保存在我已经设置的文件中。该脚本工作正常,但后来我意识到下载的某些附件已损坏。这是我使用的脚本。

Function saveattachmentexcel 
{
 $Null = Add-type -Assembly "Microsoft.Office.Interop.Outlook"
 #olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
 #olFolderInbox = 6
 $outlook = new-object -comobject outlook.application
 $namespace = $outlook.GetNameSpace("MAPI")
 $folder = $nameSpace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
 $filepath = "D:\DMR Folder\"
 $folder.Items | Where {$_.UnRead -eq $True -and $($_.attachments).filename -match '.xlsm'} | ForEach-object {
      $filename = $($_.attachments | where filename -match '.xlsm').filename
    foreach($file in $filename)
    {
        $outpath = join-path $filepath $file
        $($_.attachments).saveasfile($outpath)
    }
     $_.UnRead = $False
   }
}
saveattachmentexcel

我不知道为什么会这样。谁能帮帮我?

【问题讨论】:

    标签: powershell shell outlook automation


    【解决方案1】:

    这可能是因为您尝试使用$($_.attachments).saveasfile($outpath) 语句将每个附件保存到磁盘上的相同文件名。

    改变这个:

    $filename = $($_.attachments | where filename -match '.xlsm').filename
    foreach($file in $filename)
    {
        $outpath = join-path $filepath $file
        $($_.attachments).saveasfile($outpath)
    }
    

    到:

    foreach($attachment in $_.attachments)
    {
        if($attachment.Filename -like '*.xlsm'){
            $outpath = Join-Path $filepath $attachment.Filename
            # Only save this particular attachment to disk - not all of them
            $attachment.SaveAsFile($outpath)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      相关资源
      最近更新 更多