2
votes

Why am I getting the error for the code below:

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

Receive-Job : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its propertie s do not match any of the parameters that take pipeline input. At line:1 char:147 + Get-Job -Id 1 | Select-Object -ExpandProperty childjobs | Where-Object {$_.state -eq 'Completed'} | Select-Object -ExpandProperty id | Receive-Job <<<<
+ CategoryInfo : InvalidArgument: (2:PSObject) [Receive-Job], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.ReceiveJobCommand

However, this line works perfectly:

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

Any tips or helpful comments on the code are appreciated. I'm new to PowerShell.

Thanks

1

1 Answers

4
votes

The problem is that Select-Object -ExpandProperty id is sending a System.Int32 which isn't what Receive-Job is expecting for it's ISA/HASA binding. Remove -ExpandProperty so you keep the System.Management.Automation.PSCustomObject with the ID property.

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

If you want to see the nitty gritty details of why a System.Int32 didn't bind to Receive-Job you can use Trace-Command. This simplified example tries to bind a Int32 (current process ID) to Get-Process.

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

The output of this command is lengthy but it shows you everything PowerShell tried to do to bind the upstream object to the downstream cmdlet.