CmdLets/Master/Watch-Process.ps1
|
Function Watch-Process { <# .SYNOPSIS Monitors a running process by its ID and outputs periodic status updates until the process terminates. .DESCRIPTION The Watch-Process function accepts a process object and enters a monitoring loop. It checks if the process is still active at one-second intervals using Get-Process. While running, it outputs a yellow status message every few seconds (with a gradually increasing delay interval up to 30 seconds to reduce console noise), and prints a green completion message once the process exits. .PARAMETER Process A mandatory process object (typically returned by Get-Process or Start-Process) to monitor. .EXAMPLE $Proc = Start-Process notepad.exe -PassThru Watch-Process -Process $Proc Description: Launches Notepad, passes the process object to Watch-Process, and monitors it until the application is closed. .NOTES Prerequisites: - Requires a valid process object containing an ID property. .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-process #> [CmdletBinding()] Param( [Parameter(Mandatory=$True)] $Process ) if ($Process) { $LastWrite = Get-Date $WriteEvery = 5 Do { if ((New-TimeSpan -Start $LastWrite -end (Get-Date)).Seconds -ge $WriteEvery) { $LastWrite = Get-Date Write-Host "`tProcess Running: $($Process.Name)" -ForegroundColor Yellow if ($WriteEvery -lt 30) { $WriteEvery++ } } Start-Sleep -Seconds 1 } while (Get-Process -id $Process.ID -ErrorAction SilentlyContinue) Write-Host "`tProcess Complete: $($Process.Name)" -ForegroundColor Green } else { Write-Host "`tProcess Complete:" -ForegroundColor Cyan } } |