运行并行作业并在PowerShell脚本中连续传输其输出
我有一个PowerShell脚本(通过WinRM运行),它不断地向调用者输出一些信息。在某些情况下,它会运行一个可执行文件(持续几分钟): 我希望与exe并行执行另一项工作,该工作还将持续向脚本调用方输出一些信息:运行并行作业并在PowerShell脚本中连续传输其输出,powershell,Powershell,我有一个PowerShell脚本(通过WinRM运行),它不断地向调用者输出一些信息。在某些情况下,它会运行一个可执行文件(持续几分钟): 我希望与exe并行执行另一项工作,该工作还将持续向脚本调用方输出一些信息: While (exe not finished) { Write-Output "Some info" Start-Sleep -Seconds 1 } 调用者应该收到每一行消息 Starting Some info Some info Some info Fini
While (exe not finished) {
Write-Output "Some info"
Start-Sleep -Seconds 1
}
调用者应该收到每一行消息
Starting
Some info
Some info
Some info
Finished
一旦它们被执行,因为它必须在GUI上连续显示它们
我怎样才能做到这一点?我无法连续调用
Receive Job
,因为脚本正在处理可执行文件。您可以执行以下操作:
$job = Start-Job -ScriptBlock {start-sleep 20 # Your scriptblock here}
while ($job.state -eq 'running'){
Write-Output "Job still running"
Start-Sleep 1
}
最简单的解决方案是使用Jobs或RunspaceNice,谢谢!我没有考虑将作业用于可执行文件本身,尽管这一点现在看起来很明显。
$job = Start-Job -ScriptBlock {start-sleep 20 # Your scriptblock here}
while ($job.state -eq 'running'){
Write-Output "Job still running"
Start-Sleep 1
}