删除OST文件并运行Outlook进程

时间:2016-06-08 17:56:46

标签: forms powershell outlook delete-file

我有一个表单,我用来删除powershell中的ost文件,然后启动outlook。我认为启动前景的过程比ost可以删除更快。这就是我所拥有的。

    taskkill.exe /im outlook.exe /f

    $OstPath = $Env:LocalAppData + "\Microsoft" + "\Outlook"
    $ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"} 
    $ost | remove-Item

    Wait-Job $ost   

    Start-Process -FilePath "C:\Program Files\Microsoft Office 15\root\office15\outlook.exe"

1 个答案:

答案 0 :(得分:0)

这是我提出的功能,并且在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)

           }
}