首页 > 解决方案 > 已解决 - 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))

标签: powershellpst

解决方案


推荐阅读