首页 > 解决方案 > 等待..直到在 ForEach

问题描述

我有一个场景,我需要ForEach在执行命令之前循环等待。

$classList = Get-ClassList
forEach ($class in $classList) {
    $studentList = Get-StudentList -class $class.Name
    forEach ($student in $studentList) {
        Write-Host $student.Name
        $rollCall = Roll-Call -student $student.Name
    }
}

我要解决的问题是等到学生滚动。当他已经点名时,我需要得到他的输出(只是点名信息),如下:

$update = Get-RollCallOutput-student $student.Name -class $class.Name

每个学生的点名可能会有所不同。此外,输出是一个String.Object[]类型。也就是说,要获得特定的输出,我需要循环点名更新。

forEach ($output in $update) {
   Get-Output -outputID $output.Id -student $student.Name
}

按照设计,如果学生的点名完成,Get-RollCallOutput则不会返回Null。否则为空。也就是说,为了等待输出 NOT 为 NULL,我使用了While($job)

$update = Get-RollCallOutput-student $student.Name -class $class.Name
while($job)
forEach ($output in $update) {
   Get-Output -outputID $output.Id -student $student.Name
}

问题是这个在嵌套的 ForEach 里面,它总是挂起。目前,解决方法是Start-Sleep -s 15在脚本跳转查询输出之前等待点名完成。我知道这不是一个好的解决方案,并且确实会影响性能。但是,在研究更好的方法时,这是可以接受的。

[更新]即使使用Do ... While但不起作用。我总是看到它在点名命令后被挂起。

Do {
   $update = Get-RollCallOutput-student $student.Name -class $class.Name
}

While ($job) {...}

标签: powershell

解决方案


推荐阅读