2016-06-11 107 views
0

我寫了一個PowerShell腳本在我的公司使用。無論出於何種原因,應該刪除指定位置上的所有.ost文件的腳本部分只能工作一段時間。 ost的路徑不會改變。任何人有任何想法,爲什麼會這樣?需要幫助,混淆powershell問題

Stop-process -Name OUTLOOK -ErrorAction SilentlyContinue -Force 
Stop-process -Name communicator -ErrorAction SilentlyContinue -Force 
Stop-process -Name lync -ErrorAction SilentlyContinue -Force 
Stop-Process -Name UcMapi -ErrorAction SilentlyContinue -Force 
Stop-Process -Name skypehost -ErrorAction SilentlyContinue -Force 
Stop-Process -Name searchprotocalhost -ErrorAction SilentlyContinue -Force 

$OstPath = "c:\users\$([environment]::username)"+ "\AppData" + "\local" + "\Microsoft" + "\Outlook" 
$ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"} 
$ost | remove-Item -WhatIf 

Start-Process Outlook 
+2

是因爲拼錯'searchprotocolhost'嗎?因爲你使用'-WhatIf',這意味着什麼都不會被刪除?由於某些計算機未將用戶文件存儲在'c:\ users'中,例如他們沒有英文本地化?由於另一個程序可能正在使用它,例如Mitel電話系統PIM。 – TessellatingHeckler

+0

已更正,但仍存在問題。 – Funkel

+0

現在已解決。我刪除了-whatif並添加了強制,並且每次都清除它。我不好提前不試。感謝您的幫助! – Funkel

回答

2

你會想改變你的代碼,類似於下面的東西。我在我的測試環境中測試了下面的內容,並且它沒有問題。

$OSTPath = Get-Item -Path "$env:USERPROFILE\AppData\Local\Microsoft\Outlook\*" -Filter '*.ost' 

If ($OSTPath) 

    { 
     Write-Output "OST File Found, Deleting File...." 
      Remove-Item -Path $OSTPath -Force 
    } 

ELSE 

    { 
     Write-Output "No OST Exists. No Action Taken." 
    } 

由於沒有發佈解決方案,我想我會爲任何可能遇到此問題的人添加解決方案。

+0

這是獲取更可讀性路徑的更好方法。 – user4317867