2

为什么我收到以下代码的错误:

Get-Job -Id 1 | Select-Object -ExpandProperty childjobs | Where-Object {$_.state -eq 'Completed'} | Select-Object -ExpandProperty id | Receive-Job 

Receive-Job :输入对象不能绑定到命令的任何参数,因为命令不接受管道输入,或者输入及其属性不匹配任何接受管道输入的参数。在 line:1 char:147 + Get-Job -Id 1 | 选择对象-ExpandProperty 子作业 | Where-Object {$_.state -eq 'Completed'} | 选择对象-ExpandProperty id | Receive-Job <<<<
+ CategoryInfo : InvalidArgument: (2:PSObject) [Receive-Job], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ReceiveJobCommand

但是,这条线完美地工作:

Receive-Job (Get-Job -Id 1 | Select-Object -ExpandProperty childjobs | Where-Object {$_.state -eq 'Completed'} | Select-Object -ExpandProperty id )

感谢您对代码的任何提示或有用的评论。我是 PowerShell 的新手。

谢谢

4

1 回答 1

4

问题是Select-Object -ExpandProperty id发送的System.Int32不是Receive-JobISA/HASA 绑定所期望的。删除-ExpandProperty,以便您保留System.Management.Automation.PSCustomObjectID 属性。

Get-Job -Id 1 | Select-Object -ExpandProperty childjobs | Where-Object {$_.state -eq 'Completed'} | Select-Object id | Receive-Job

如果您想查看为什么 aSystem.Int32没有绑定到Receive-Job您的详细信息,可以使用Trace-Command. 这个简化的示例尝试将Int32(当前进程 ID)绑定到Get-Process.

Trace-Command -Name ParameterBinding  -Option All -Expression { $PID | Get-Process } -PSHost

此命令的输出很长,但它向您展示了 PowerShell 为将上游对象绑定到下游 cmdlet 所做的一切尝试。

于 2012-03-16T16:02:27.087 回答