1

Following up to this question, it seems like Select-Object sets its input to null as part of its processing. This seems wrong to me. Here's what I'm trying to do:

$sessionInput = new-object -TypeName System.Object
$sessionInput | Add-Member -MemberType NoteProperty -Name Foo -Value $foo
$sessionInput | Add-Member -MemberType NoteProperty -Name Bar -Value $bar
Invoke-Command -Session $session -InputObject $sessionInput {
    $foo = $input | Select-Object -ExpandProperty Foo
    $bar = $input | Select-Object -ExpandProperty Bar

    # Expected: $foo, $bar inside the session = $foo, $bar outside the session
}

What actually happens is that only $foo has its expected value, and $bar is always $null. After a little investigation, it turns out that $input is set to $null after the first Select-Object runs. As an example, inserting $input | Get-Member in between the two Select-Object lines throws an error stating that "No object has been specified to the get-member cmdlet."

What is going on here?

Community
  • 1
  • 1
alastairs
  • 6,697
  • 8
  • 50
  • 64

2 Answers2

2

The type of $input in this instance is [System.Management.Automation.Runspaces.PipelineReader`1+GetReadEnumerator>d__0[[System.Object]]]. Doing $inputParameters = $input | Select-Object to read the object out of the pipeline and stash it away has the desired effect: $inputParameters has type PSCustomObject and can be poked further multiple times by calls to Select-Object.

alastairs
  • 6,697
  • 8
  • 50
  • 64
0

Does this work? specifying $SomeVar = $Input and then calling that instead?

$sessionInput = new-object -TypeName System.Object
$sessionInput | Add-Member -MemberType NoteProperty -Name Foo -Value $foo
$sessionInput | Add-Member -MemberType NoteProperty -Name Bar -Value $bar
Invoke-Command -Session $session -InputObject $sessionInput {
    $TempInput = $input
    $foo = $TempInput | Select-Object -ExpandProperty Foo
    $bar = $TempInput | Select-Object -ExpandProperty Bar

    # Expected: $foo, $bar inside the session = $foo, $bar outside the session
}
HungryHippos
  • 1,473
  • 5
  • 16
  • 24