Powershell - 寻找一种更好的方法来检查多个进程是否已完成
Powershell - Looking for a better way to check if multple processes have completed
我有一个脚本可以使用 Start-Process 命令创建一些进程。可能有数百个进程 运行。该脚本工作得很好,但我希望有人能为我提供一种更优雅的方法来检查我开始的流程是否已全部完成?一个不需要任何输入的方法,因为我正在寻找一个自动化的解决方案。
下面是一个代码示例。
$procList = @()
$cmd = "C:\Windows\SysWOW64\PING.EXE"
$cmdArgs = " nos3nse.com"
$proc1 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$proc2 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$proc3 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$procObject = New-Object psobject -Property @{
id = $proc1.Id
name = $proc1.Name
startTime = $proc1.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
$procObject = New-Object psobject -Property @{
id = $proc2.Id
name = $proc2.Name
startTime = $proc2.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
$procObject = New-Object psobject -Property @{
id = $proc3.Id
name = $proc3.Name
startTime = $proc3.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
# Is there a better way to find out if these jobs have all completed?
$ppFlag = $true
while ($ppFlag) {
foreach ($proc in $procList) {
$chkJob = Get-Process -id $proc.id -ErrorAction SilentlyContinue
if ($chkJob.HasExited -eq $false)
{
Write-Host ("looping")
Write-Host ("Sleeping 2 seconds") -ForegroundColor Yellow
Start-Sleep -Seconds 2
break #break foreach loop
}
} #foreach
if ($chkJob.HasExited -ne $false) { $ppFlag = $false }
} #while
这个怎么样?
$a = start -NoNewWindow powershell {timeout 10; 'a done'} -PassThru
$b = start -NoNewWindow powershell {timeout 10; 'b done'} -PassThru
$c = start -NoNewWindow powershell {timeout 10; 'c done'} -PassThru
$a,$b,$c | wait-process
Waiting for 0 seconds, press a key to continue ...
Waiting for 0 seconds, press a key to continue ...
Waiting for 0 seconds, press a key to continue ...
a done
b done
c done
等待过程可以与数组一起使用。也许你应该更新你的问题。
1..3 | foreach { start -NoNewWindow powershell {timeout 5; 'done'} -PassThru } |
wait-process
我有一个脚本可以使用 Start-Process 命令创建一些进程。可能有数百个进程 运行。该脚本工作得很好,但我希望有人能为我提供一种更优雅的方法来检查我开始的流程是否已全部完成?一个不需要任何输入的方法,因为我正在寻找一个自动化的解决方案。
下面是一个代码示例。
$procList = @()
$cmd = "C:\Windows\SysWOW64\PING.EXE"
$cmdArgs = " nos3nse.com"
$proc1 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$proc2 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$proc3 = Start-Process $cmd -ArgumentList $cmdArgs -PassThru
$procObject = New-Object psobject -Property @{
id = $proc1.Id
name = $proc1.Name
startTime = $proc1.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
$procObject = New-Object psobject -Property @{
id = $proc2.Id
name = $proc2.Name
startTime = $proc2.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
$procObject = New-Object psobject -Property @{
id = $proc3.Id
name = $proc3.Name
startTime = $proc3.StartTime
dtlLogFile = $dtlLogFileTemp
}
$procList += $procObject
# Is there a better way to find out if these jobs have all completed?
$ppFlag = $true
while ($ppFlag) {
foreach ($proc in $procList) {
$chkJob = Get-Process -id $proc.id -ErrorAction SilentlyContinue
if ($chkJob.HasExited -eq $false)
{
Write-Host ("looping")
Write-Host ("Sleeping 2 seconds") -ForegroundColor Yellow
Start-Sleep -Seconds 2
break #break foreach loop
}
} #foreach
if ($chkJob.HasExited -ne $false) { $ppFlag = $false }
} #while
这个怎么样?
$a = start -NoNewWindow powershell {timeout 10; 'a done'} -PassThru
$b = start -NoNewWindow powershell {timeout 10; 'b done'} -PassThru
$c = start -NoNewWindow powershell {timeout 10; 'c done'} -PassThru
$a,$b,$c | wait-process
Waiting for 0 seconds, press a key to continue ...
Waiting for 0 seconds, press a key to continue ...
Waiting for 0 seconds, press a key to continue ...
a done
b done
c done
等待过程可以与数组一起使用。也许你应该更新你的问题。
1..3 | foreach { start -NoNewWindow powershell {timeout 5; 'done'} -PassThru } |
wait-process