这是我想出了功能和作品真的很好与Office 2013和Office 2016
Function RebuildEmail
{
#COMMENT: Stops all Outlook process' and deletes .ost file then prompts to run setup.
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("This will stop Outlook and Skype for Business,`nRemoves the local copy of your email and will resynchronize from the exchange server. Be sure you have a steady internet connection! . `nDO YOU WANT TO CONTINUE?", `
0,"Service Desk",4)
If ($intAnswer -ne 6) {
$a.popup("Please call the Service Desk for support.")
} else {
$a.popup("Rebuilding will begin...")
Stop-Process -Name ("Outlook", "Lync")
$n = 0
Do
{
If(Get-Process -Name ("outlook", "Lync") -ErrorAction silentlycontinue)
{
Stop-Process -Name ("Outlook", "Lync") -ErrorAction SilentlyContinue
If ($n -lt 5)
{
$ExitLoop = $false
$n++
Start-Sleep -Seconds 3
If($n -eq 5){$ExitLoop = $true}
}
}
else {$ExitLoop = $true}
}
While ($ExitLoop -eq $false)
if ($n -ne 5)
{
$OstPath = $Env:LocalAppData + "\Microsoft" + "\Outlook"
$OSTs = Get-ChildItem $OstPath | where { $_.Extension -eq ".ost"}
$OSTs | Remove-Item
Start-Sleep -s 3
foreach ($ost in $OSTs)
{
$FilePath = $OstPath + "\" + $ost
$n = 0
Do
{
if(Test-Path $FilePath)
{
Remove-Item $FilePath
Start-Sleep -Seconds 5
$ExitLoop = $false
$n++
if($n -eq 5){$ExitLoop = $true}
}
else {$ExitLoop = $true}
}
While ($ExitLoop -eq $false)
if($n -ne 5)
{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("The local copy of your email was successfully removed.", `
0,"Service Desk",0)
}
else
{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Unfortunatley, your local copy couldn't be removed. Please call the Service Desk.", `
0,"Service Desk",0)
}
}
}
else
{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Outlook was unable to close. Please call the Service Desk.", `
0,"Service Desk",0)
}
Start-Process -FilePath "C:\Program Files\Microsoft Office 15\root\office15\outlook.exe" -ErrorAction SilentlyContinue
Start-Process -FilePath "C:\Program Files (x86)\Microsoft Office\root\Office16\outlook.exe" -ErrorAction SilentlyContinue
Start-Process -FilePath "C:\Program Files\Microsoft Office 15\root\office15\lync.exe" -ErrorAction SilentlyContinue
Start-Process -FilePath "C:\Program Files (x86)\Microsoft Office\root\Office16\lync.exe" -ErrorAction SilentlyContinue
Start-Sleep -s 30
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Please allow a few minutes to update folders. Call the Service Desk if issue persist.", `
0,"Service Desk",0)
}
}
_I想启动Outlook的过程是开放比OST快可以deleted_这是否意味着你得到一个错误?你能确认文件正在被删除吗?我会在taskkill之后睡一觉,因为从经验来看,这些文件会在outlook被终止后继续保持这些文件持续~10s。 – Matt
我不认为杀死outlook.exe进程是个好主意。我会建议抓住它的COM实例(如果正在运行)并告诉它退出。 –