【问题标题】:How to disconnect PST file from Outlook using Powershell?如何使用 Powershell 断开 PST 文件与 Outlook 的连接?
【发布时间】:2014-07-26 08:20:29
【问题描述】:

我正在尝试编写一个脚本来断开 PST 文件与 Outlook 的连接。

我一直在尝试这样的事情:

$Outlook = new-object -com outlook.application 
$Namespace = $Outlook.getNamespace("MAPI")

$PSTtoDelete = "c:\test\pst.pst"

$Namespace.RemoveStore($PSTtoDelete)

我收到以下错误:

“无法找到“RemoveStore”和参数计数“1”的重载。

我也尝试了一个不同的解决方案(在这里找到http://www.mikepfeiffer.net/2013/04/how-to-test-outlook-pst-personal-folder-file-access-with-powershell/):

$namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($PSTFolder))

我查看了technect documentations,如果我理解正确,RemoveStore 方法需要一个文件夹。

如果有人能给我一个关于这个的提示,将不胜感激!

谢谢!

【问题讨论】:

    标签: powershell outlook pst


    【解决方案1】:

    要删除所有 .pst 文件:

    $Outlook = New-Object -ComObject Outlook.Application
    $Namespace = $Outlook.getNamespace("MAPI")
    
    $all_psts = $Namespace.Stores | Where-Object {($_.ExchangeStoreType -eq '3') -and ($_.FilePath -like '*.pst') -and ($_.IsDataFileStore -eq $true)}
    
    ForEach ($pst in $all_psts){
        $Outlook.Session.RemoveStore($pst.GetRootFolder())
    }
    

    【讨论】:

      【解决方案2】:

      根据您的链接,脚本需要附加 PST 的 Name,而不是路径。试试这个:

      $Outlook = new-object -com outlook.application 
      $Namespace = $Outlook.getNamespace("MAPI")
      
      $PSTtoDelete = "c:\test\pst.pst"
      $PST = $namespace.Stores | ? {$_.FilePath -eq $PSTtoDelete}
      $PSTRoot = $PST.GetRootFolder()
      
      
      $PSTFolder = $namespace.Folders.Item($PSTRoot.Name)
      $namespace.GetType().InvokeMember('RemoveStore',[System.Reflection.BindingFlags]::InvokeMethod,$null,$namespace,($PSTFolder))
      

      【讨论】:

      • 如果 PST 与主帐户同名,则 $PSTFolder 是错误的对象。 RemoveStore 不起作用。
      • RemoveStore 不需要名称(字符串)。它需要一个 Store 对象的实例。
      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多