Select-Object是否修改输入?

时间:2012-03-21 18:54:58

标签: powershell powershell-v2.0 powershell-remoting

跟随this question,似乎Select-Object将其输入设置为null,作为其处理的一部分。这对我来说似乎不对。这就是我想要做的事情:

$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
}

实际发生的是,只有$foo具有预期值,而$bar始终为$null。经过一些调查后发现,$input在第一次$null运行后设置为Select-Object。例如,在两个$input | Get-Member行之间插入Select-Object会抛出一个错误,指出“没有为get-member cmdlet指定任何对象。”

这里发生了什么?

2 个答案:

答案 0 :(得分:2)

此实例中的$ input类型为[System.Management.Automation.Runspaces.PipelineReader`1+GetReadEnumerator>d__0[[System.Object]]]。执行$inputParameters = $input | Select-Object将对象从管道中读出并将其存放起来会产生预期的效果:$inputParameters类型为PSCustomObject,并且可以通过调用Select-Object进一步调整对象

答案 1 :(得分:0)

这有用吗?指定$ SomeVar = $ Input然后再调用它?

$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
}