alastairs
alastairs

Reputation: 6805

Does Select-Object modify the input?

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?

Upvotes: 1

Views: 311

Answers (2)

alastairs
alastairs

Reputation: 6805

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.

Upvotes: 2

HungryHippos
HungryHippos

Reputation: 1543

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
}

Upvotes: 0

Related Questions