When code is invoked though Invoke-Command
using the -ScriptBlock
, -ArgumentList
and -Computer
parameters, only a single item is returned from each call to the servers.
Two examples can be found below highlighting the problem.
$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
Can anyone explain to me what i am doing wrong? I cant be the only person caught out by this.