Invoke-Command returns 使用 ScriptBlock 和 ArgumentList 调用时只有一个对象
Invoke-Command returns only a single object when called using ScriptBlock and ArgumentList
当使用 -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
任何人都可以向我解释我做错了什么吗?我不可能是唯一被这个抓住的人。
-ArgumentList
顾名思义,它将参数列表传递给命令。如果可能,该列表中的每个值都分配给定义的参数。但是你只定义了一个参数:$array
。因此,您只能从 arg 列表中获取第一个值。
看,这实际上是它应该如何工作的(3 个参数绑定到 3 个参数):
Invoke-Command -Session $s -ScriptBlock {
param ($p1, $p2, $p3)
$p1, $p2, $p3 | % { $i = $_ ; Get-culture | select @{name='__id'; ex={$i} } , DisplayName }
} -ArgumentList 1, 2, 3
所以,您真正想要做的是将 one 数组作为 one 单个参数传递。
实现该目标的一种方法是:
-ArgumentList (,(1, 2, 3))
最终代码:
Invoke-Command -Session $s -ScriptBlock {
param ($array)
$array | % { $i = $_ ; Get-culture | select @{n = '__id'; e = {$i}}, DisplayName }
} -ArgumentList (, (1, 2, 3))
另一种方法(在这个简单的例子中)是使用 automatic $args
变量:
Invoke-Command -ScriptBlock {
$args | % { $i = $_ ; Get-culture | select @{n = '__id'; e = {$i}}, DisplayName }
} -ArgumentList 1, 2, 3
当使用 -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
任何人都可以向我解释我做错了什么吗?我不可能是唯一被这个抓住的人。
-ArgumentList
顾名思义,它将参数列表传递给命令。如果可能,该列表中的每个值都分配给定义的参数。但是你只定义了一个参数:$array
。因此,您只能从 arg 列表中获取第一个值。
看,这实际上是它应该如何工作的(3 个参数绑定到 3 个参数):
Invoke-Command -Session $s -ScriptBlock {
param ($p1, $p2, $p3)
$p1, $p2, $p3 | % { $i = $_ ; Get-culture | select @{name='__id'; ex={$i} } , DisplayName }
} -ArgumentList 1, 2, 3
所以,您真正想要做的是将 one 数组作为 one 单个参数传递。
实现该目标的一种方法是:
-ArgumentList (,(1, 2, 3))
最终代码:
Invoke-Command -Session $s -ScriptBlock {
param ($array)
$array | % { $i = $_ ; Get-culture | select @{n = '__id'; e = {$i}}, DisplayName }
} -ArgumentList (, (1, 2, 3))
另一种方法(在这个简单的例子中)是使用 automatic $args
变量:
Invoke-Command -ScriptBlock {
$args | % { $i = $_ ; Get-culture | select @{n = '__id'; e = {$i}}, DisplayName }
} -ArgumentList 1, 2, 3