我正在尝试使用 PowerShell 脚本从文件夹中的 Outlook 项目中提取附件。代码是:`
# Specify the folder containing Outlook item files (.msg)
$sourceFolder = "C:\GRAINGER\GRAINGER\06-07-2023 Placements"
# Specify the parent folder where you want to save attachments
$saveFolder = "C:\GRAINGER\GRAINGER\Attachments\06-07-2023 Placements A"
# Load the Outlook COM Object
Add-Type -TypeDefinition @"
using System;
using Microsoft.Office.Interop.Outlook;
"@
# Create an Outlook Application object
$outlook = New-Object -ComObject Outlook.Application
# Get a list of .msg files in the source folder
$msgFiles = Get-ChildItem $sourceFolder -Filter *.msg
# Loop through each .msg file and extract attachments
foreach ($msgFile in $msgFiles) {
$mailItem = $outlook.Session.OpenSharedItem($msgFile.FullName)
# Create a folder with the same name as the Outlook item file
$itemFolder = New-Item -Path "$saveFolder\$($msgFile.BaseName)" -ItemType Directory
# Loop through each attachment in the email
foreach ($attachment in $mailItem.Attachments) {
$attachment.SaveAsFile("$itemFolder\$($attachment.FileName)")
}
# Close the mail item
$mailItem.Close()
}
# Release Outlook objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($attachment) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($mailItem) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
我不断收到如下错误: 类型或命名空间名称“Office”确实 命名空间“Microsoft”中不存在(您是否缺少程序集引用?) CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [添加类型],除外 离子 +FullyQualifiedErrorId:SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
我安装了office 365,还安装了outlook。但我被困在这里...
您需要先添加互操作dll
Add-Type -AssemblyName "Microsoft.Office.Interop.Outlook"
或者您可以跳过整个
Add-Type -TypeDefinition @"
块并使用后期绑定而不依赖互操作。