Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在使用powershell从outlook下载时修复损坏的文件_Powershell_Shell_Outlook_Automation - Fatal编程技术网

如何在使用powershell从outlook下载时修复损坏的文件

如何在使用powershell从outlook下载时修复损坏的文件,powershell,shell,outlook,automation,Powershell,Shell,Outlook,Automation,我有一个powershell脚本,可以从outlook自动下载并保存在我已经设置的文件中。脚本工作正常,但后来我意识到下载的一些附件已损坏。这是我使用的脚本 Function saveattachmentexcel { $Null = Add-type -Assembly "Microsoft.Office.Interop.Outlook" #olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFold

我有一个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

我不知道为什么会这样。有人能帮我吗?

这可能是因为您试图使用
$($.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)
    }
}