2016-06-11 115 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