Start-Job没有产生预期的产出

时间:2017-12-24 19:32:29

标签: powershell scripting

为什么在下面的代码中,在作业输出之前显示Read-Host行的输出?

$WorkingDirectory = Get-Location

$ScriptBlockForJob = {    
    Get-ChildItem $Input -Directory `
        | Where-Object `
            {$_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0} `
                | Select-Object FullName                
}

Start-Job -InputObject $WorkingDirectory -ScriptBlock $ScriptBlockForJob | Wait-Job | Receive-Job 

Read-Host 'Above is a list of empty directories. Press enter to begin deleting them'

The code above yields this ouput

1 个答案:

答案 0 :(得分:0)

如果您删除$ScriptBlockForJob中的最后一行,该命令将按预期运行。

$ScriptBlockForJob = {    
    Get-ChildItem $Input -Directory `
        | Where-Object `
            {$_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0}                               
}

Output as expected