I wrote a quick powershell function for tracking if a process is running. I've found this useful for cases where I want my script to wait for a process to complete before continuing, or for providing 'visual' feedback that work is occurring.
In the sample below, the process I'm tracking with the function is "robocopy". (as declared with $Process=)
----------------------------------------------Begin Script-------------------------------------------------------------
$Process="Robocopy"
Function Check-Process-Alive
{
<#
.Synopsis
Checks if a process is running, and updates the screen with a new "line" for each second the process continues to run.
.Description
This particular function is helpful to see when a long running process stops.
.Example
PS C:\> Check-Process-Alive %processname%
#>
Param ($X)
While (Get-Process $X -ErrorAction SilentlyContinue)
{
Write-Host "Copying files..." -foregroundcolor Green
Start-Sleep -s 10
}
} # End function
Check-Process-Alive $Process
-------------------------------------------------End Script-----------------------------------------------------------
This code essentially allows you to call off a process in your script (an install or a file copy for instance) and your script will then 'pause' in essence while waiting for the process you launched to complete.
No comments:
Post a Comment