Start-Job没有产生预期的输出[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

为什么在下面的代码中,在作业输出之前显示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

powershell scripting
1个回答
0
投票

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

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

Output as expected

© www.soinside.com 2019 - 2024. All rights reserved.