需要帮助解决令人困惑的powershell问题

需要帮助解决令人困惑的powershell问题,powershell,outlook,Powershell,Outlook,我写了一个powershell脚本供公司使用。无论出于何种原因,脚本中用于删除指定位置处所有.ost文件的部分只在某些时候起作用。ost的路径不会更改。有人知道为什么会这样吗 Stop-process -Name OUTLOOK -ErrorAction SilentlyContinue -Force Stop-process -Name communicator -ErrorAction SilentlyContinue -Force Stop-process -Name lync -Erro

我写了一个powershell脚本供公司使用。无论出于何种原因,脚本中用于删除指定位置处所有.ost文件的部分只在某些时候起作用。ost的路径不会更改。有人知道为什么会这样吗

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

您需要将代码更改为类似于下面的内容。我在我的测试环境中测试了下面的代码,它工作正常

$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."
    }

由于解决方案未发布,我想我会为可能遇到此问题的任何人添加一个解决方案。

您将希望将代码更改为类似于下面的内容。我在我的测试环境中测试了下面的代码,它工作正常

$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."
    }

由于解决方案未发布,我想我会为可能遇到此问题的任何人添加一个解决方案。

是因为您拼写错误了
searchprotocolhost
?因为您使用了
-WhatIf
,这意味着没有任何内容被删除?因为您的某些计算机没有将用户文件存储在
c:\users
中,例如,它们不是英语本地化的?因为另一个程序可能正在使用它,例如Mitel电话系统PIM。已更正,但仍存在问题。此问题现已解决。我移除了-whatif并添加了-force,它每次都会清除它。我很抱歉没有提前尝试。谢谢你的帮助!是因为您拼错了
searchprotocolhost
?因为您使用了
-WhatIf
,这意味着没有任何内容被删除?因为您的某些计算机没有将用户文件存储在
c:\users
中,例如,它们不是英语本地化的?因为另一个程序可能正在使用它,例如Mitel电话系统PIM。已更正,但仍存在问题。此问题现已解决。我移除了-whatif并添加了-force,它每次都会清除它。我很抱歉没有提前尝试。谢谢你的帮助!这是获得更可读路径的更好方法。这是获得更可读路径的更好方法。