PowerShell has a built-in support to wait until a process or many processes end: simply use Wait-Process.
Understanding Data Marts and Dimensional Modeling
There is no support to do the opposite: wait until a process has started. Here is a function that can wait for any process:
#requires -Version 1
function Wait-ForProcess
{
param
(
$Name = 'notepad',
[Switch]
$IgnoreAlreadyRunningProcesses
)
if ($IgnoreAlreadyRunningProcesses)
{
$NumberOfProcesses = (Get-Process -Name $Name -ErrorAction SilentlyContinue).Count
}
else
{
$NumberOfProcesses = 0
}
Write-Host "Waiting for $Name" -NoNewline
while ( (Get-Process -Name $Name -ErrorAction SilentlyContinue).Count -eq $NumberOfProcesses )
{
Write-Host '.' -NoNewline
Start-Sleep -Milliseconds 400
}
Write-Host ''
}
So when you run this line, PowerShell will pause until you launch a new instance of Notepad:
Wait-ForProcess -Name notepad -IgnoreAlreadyRunningProcesses
Handling Already Running Processes in PowerShell
When you omit -IgnoreAlreadyRunningProcesses, then PowerShell would continue immediately if there was at least one instance of Notepad already running.
ReTweet this Tip!