【发布时间】:2019-03-21 21:52:42
【问题描述】:
当使用-ScriptBlock、-ArgumentList 和-Computer 参数通过Invoke-Command 调用代码时,每次调用服务器时只会返回一个项目。
可以在下面找到两个突出问题的示例。
$s = New-PSSession -ComputerName Machine01, Machine02
# when called, this block only retuns a single item from the script block
# notice that the array variable is being used
Invoke-Command -Session $s -ScriptBlock {
param( $array )
$array | % { $i = $_ ; Get-culture | select @{name='__id'; ex={$i} } , DisplayName
}
} -ArgumentList 1,2,3
write-host "`r`n======================================`r`n"
# when called, this block retuns all items from the script block
# notice that the call is the same but instead of using the array variable we use a local array
Invoke-Command -Session $s -ScriptBlock {
param( $array )
1,2,3 | % { $i = $_ ; Get-culture | select @{name='__id'; ex={$i} } , DisplayName
}
} -ArgumentList 1,2,3
$s | Remove-PSSession
谁能向我解释我做错了什么?我不能是唯一一个被这个抓住的人。
【问题讨论】:
标签: powershell powershell-remoting