2014-06-05 40 views
1

我想寫一個腳本來從Outlook斷開PST文件。如何使用Powershell從Outlook斷開PST文件?

我一直在試圖像這樣的東西:

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

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

$Namespace.RemoveStore($PSTtoDelete) 

我得到以下錯誤:

"Cannot Find overload for "RemoveStore" and the argument count "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方法需要一個文件夾。

如果有人能夠給我一個暗示,這將是不勝感激!

謝謝!

回答

2

根據你的鏈接腳本預計所附PST的名稱,而不是路徑。試試這個:

$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)) 
+0

感謝很多:)它的工作直! – xashcorex

+0

如果PST與主帳戶名稱相同,$ PSTFolder是錯誤的對象。 RemoveStore不起作用。 – Alban

+0

RemoveStore不指望名稱(字符串)。它期望Store對象的一個​​實例。 –

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()) 
} 
相關問題