使用Powershell显示存储在Windows文件夹中的电子邮件的附件大小



我需要的是保存在windows共享中的Outlook电子邮件的附件大小。我可以显示附件的名称,但是文件大小不正确,本例中应该是6357kb

POWERSHELL脚本:

$outlook = New-Object -comobject outlook.application
Get-ChildItem "C:Usersmaildownloads" -Filter *.msg |
ForEach-Object {
$msg = $outlook.Session.OpenSharedItem($_.FullName)
$msg.Attachments | Select FileName, Size
}
$outlook.Quit()
输出:

文件名 大小


电工证书-家用EIC_Ref62036711.pdf 0

希望这是我错过的非常简单的东西。

感谢

From the docs:

此信息可能并不总是可用于附件。为例如,在S/MIME消息中,实际的附件大小是未知的直到提取附件。在附件大小的情况下无法确定,此属性返回0。

您可以将附件保存到临时路径,然后获取大小:

function Select-AttachmentSizes {
param (
[Parameter(ValueFromPipeline)]
$Message
)
if ($Message.Attachments.Count -gt 0) {
$tempPath = [System.IO.Path]::GetTempFileName()
try {
foreach ($attachment in $Message.Attachments) {
$size = $attachment.Size
# if size can't be determined, save to temp file
if ($size -eq 0) {
$attachment.SaveAsFile($tempPath)
$size = (Get-Item $tempPath).Length
}
# return name and size
[PSCustomObject]@{
FileName = $attachment.FileName
Size = $size
}
}
}
finally {
Remove-Item -LiteralPath $tempPath -ea SilentlyContinue
}
}
}

用法:

$msg | Select-AttachmentSizes

相关内容

最新更新