如何在使用powershell从outlook下载时修复损坏的文件
我有一个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
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)
}
}