PowerShell code to wait for a background process to finish before continuing on with a script

While this code was originally developed for an Exchange script, you can use it for any PowerShell script where you want to make sure some background process/service is finished before proceeding on with the rest of your script.

I recently ran into a problem where a PowerShell scripted install/upgrade of Exchange 2016 server where the .NET Optimization Framework service was still running in the background, and it subsequently blocked the Exchange install/upgrade due to needed files being held open. Specifically, the error returned by the Exchange setup.exe was as follows:

 Performing Microsoft Exchange Server Prerequisite Check

    Configuring Prerequisites                                                                         COMPLETED
    Prerequisite Analysis                                                                             FAILED
     Setup can't continue with the upgrade because the mscorsvw (13656) has open files. Close the process, and then restart Setup.
     For more information, visit: http://technet.microsoft.com/library(EXCHG.150)/ms.exch.setupreadiness.ProcessNeedsToBeClosedOnUpgrade.aspx

NOTE: The process number noted for the mscorsvw process is irrelevant as it will change each time it is run. Also you can change the process name to be that of any process you want to monitor with a countdown timer.

One option was to keep re-running the PowerShell script trying to perform the Exchange install/upgrade over and over again until the .NET Optimization Service (AKA mscorsvw) completed, but that got old really quick as it takes the Exchange setup process a while to generate that error each time. 😊 Another and better option was to add a check in the PowerShell script to make sure the service had ended before proceeding on with executing the Exchange install/upgrade.

To that point I wrote the following code to place right before the call to the Exchange SETUP.EXE:

 
# Check to see if the .NET Optimization Service is still running as it can block the Exchange initialization process.
If (Get-Process mscorsvw -ErrorAction SilentlyContinue) {
    # It is so define the number of minutes the countdown timer should allow it to gracefully finish before giving up and exiting the script.
    $CountDownTimer = 10
    Write-Host " "
    Write-Warning "The .NET Optimization Service is still running. Waiting up to $CountDownTimer minutes for it to gracefully shut down so the script can proceed."
    $NETOptimizationRunning = $True
    $StartTime = Get-Date
    $EndTime = $StartTime.AddMinutes($CountDownTimer)
    $TimeSpan = New-TimeSpan (Get-Date) $EndTime
}
# While the process is still running, or there is time remaining in the timespan countdown, loop through writing a progress bar and check to see if the process is still running.
While ($NETOptimizationRunning -and ($TimeSpan.TotalSeconds -gt 0)) {
    Write-Progress -Activity "Waiting for the .NET Optimization Service to shut down." -Status "$CountDownTimer minute countdown before the script gives up and exits." `
        -SecondsRemaining $TimeSpan.TotalSeconds
    Start-Sleep -Seconds 1
    # Recheck to see if the process is still running. It will return a $Null/$False value if it isn't for evaluation at the beginning of the loop.
    $NETOptimizationRunning = Get-Process mscorsvw -ErrorAction SilentlyContinue
    # Recalculate the new timespan at the end of the loop for evaluation at the beginning of the loop.
    $TimeSpan = New-TimeSpan (Get-Date) $EndTime
}
# Close out the progress bar cleanly if it was still running. 
Write-Progress -Activity "Waiting for .NET Optimization Service to shut down." -Completed
# If the .NET Optimization Service is still running after the end count time timer exit the script. Otherwise note the time it took the services to shut down if a timer was started.
If ($NETOptimizationRunning) {
    Write-Host "The .NET Optimization Service is still running after the $CountDownTimer time expired, and it can interfere with the Exchange initialization process. Please re-run this script after some time has elapsed."
    EXIT
} ElseIf ($CountDownTimer) {
    # Calculate the completed time by taking the timespan of the start time versus the end time, where the end time has the seconds the loop took to run subtracted from it.
    $CompletedTime = (New-TimeSpan $StartTime $Endtime.AddSeconds(-$TimeSpan.TotalSeconds)).ToString("mm':'ss")
    Write-Host "The .NET Optimization Service finished after $CompletedTime. Continuing with script execution."
    Write-Host " "
}
Write-Host "Moving on with the script."

This code establishes a maximum of 10 minutes (which you can change via the $CountDownTimer variable) to allow the .NET Optimization Service to complete before it gives up and exits the script. The hope is that 10 minutes will be more than sufficient for the process to finish gracefully, but if it’s still not finished after 10 minutes then its better to exit the script versus trying to execute SETUP.EXE (which will ultimately fail with the error noted above).

While the countdown is running, a progress bar is shown indicating how much time is left in the countdown timer. If the process finishes before the 10 minute window is up, the script will note how long the timer had to wait for the process to finish. From there the script will immediately continue on its merry way to the call to the SETUP.EXE (or whatever else you want to do with your script after this countdown timer).

NOTE: If the .NET Optimization Service was already completed prior to reaching this code in the PowerShell script, nothing is displayed to the user (no reason to cause confusion/panic), and again the script continues on its merry way.

I hope this script is useful to anyone who needs to ensure a background process/service is complete before moving on with the rest of the script (such as with an Exchange install/upgrade script), and if so please give it a rating and link it to whatever social media platform you feel is appropriate. Also feel free to leave any questions/comments in the section below.

Thanks,

Dan Sheehan
Senior Premier Field Engineer