powershell - 已解决 - PowerShell 在使用 Microsoft.Office.Interop.Outlook.dll 从 PST 中提取电子邮件时出现无法找到“RemoveStore”错误的重载
问题描述
我目前需要从 PST 文件中提取电子邮件信息。
我从这个链接中找到了一个 C# 示例。 https://share-my-snippets.de/snippet/291
然而,出于安全原因,我的大多数客户都不允许在他们的系统上运行已编译的二进制文件。
因此,我决定编写一个 PowerShell 脚本来执行此操作,因为客户可以查看代码并确保代码可以安全运行。
当我试图从 Outlook 默认配置文件中删除 PST 文件时,除了最后一行之外,一切运行顺利。
$nameSpace.RemoveStore($rootFolder)
是我做错了什么还是错过了什么?
谢谢你的帮助!
$pstPath = 'D:\Export\EV Export PST\Alex.pst'
$outlook = New-Object -ComObject Outlook.Application
$nameSpace = $outlook.GetNamespace('MAPI')
$pstStore = $nameSpace.Stores | Where-Object {$_.FilePath -eq $pstPath}
if (-not $pstStore) {
$nameSpace.AddStoreEx($pstPath, "olStoreDefault")
$pstStore = $nameSpace.Stores | Where-Object {$_.FilePath -eq $pstPath}
}
$rootFolder = $pstStore.GetRootFolder()
foreach ($subFolder in $rootFolder.Folders) {
Write-Host ("Sub folder in [{0}] : {1}" -f $PstDisplayName, $subFolder.FolderPath)
if ($subFolder.FolderPath -match '\\Inbox') {
foreach ($item in $subFolder.Items) {
if ($item -isnot [Microsoft.Office.Interop.Outlook.MailItem]) {
Write-Host "This is a not an email item, skipped."
continue
}
Write-Host ("Fetched item from folder: [{0}]
SenderName: {1}
SenderEmailAddress: {2}
To: {3}
CC: {4}
BCC: {5}
Size: {6}
ReceivedTime: {7}
Subject: {8}
" -f $subFolder.FolderPath, $item.SenderName, $item.SenderEmailAddress, $item.To, $item.CC, $item.BCC, $item.Size, $item.ReceivedTime, $item.Subject)
}
}
}
# issue has been resolved by replace this line to the next 2 lines
#$nameSpace.RemoveStore($rootFolder)
$pstStoreToRemove = $nameSpace.Folders.Item($rootFolder.Name)
$nameSpace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$nameSpace,($pstStoreToRemove))
解决方案
推荐阅读
- swift - 收到“无效的 Sec-WebSocket-Accept 响应”以连接到 Web 套接字
- ios - 不了解导致 StoreKit SKErrors 的原因
- unity3d - Unity:在专用服务器上通过网络进行旋转同步
- firebase - 如何使用 Firebase 云功能从右到左发送电子邮件
- c# - C# 混合两个列表
- python - ValueError: x and y must have same first dimension 抛出异常,但 x 和 y 的类型和长度相同
- django - Django Order_by 不在 FloatField 上工作
- tensorflow - Tensoflow CPU:分配超过系统内存的 10%
- javascript - ErrorAccessDenied 使用 microsoft graph 删除附件
- asp.net-core - NPOI直接将excel导出到前端而不保存在服务器上