Using sessions with Invoke-Command

Hi all

i have come through a confusion between following two commands


$session = New-PSSession -ComputerName KASUNRAJAPAKSE,localhost

Id Name ComputerName State ConfigurationName Availability

2 Session2 localhost Opened Microsoft.PowerShell Available
1 Session1 KASUNRAJAPAKSE Opened Microsoft.PowerShell Available

Invoke-Command -Command { Get-WmiObject -Class win32_ComputerSystem } -Session ( Get-PSSession -ComputerName localhost )

when i run the above command it gives me the following error Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At line:1 char:80 + Invoke-Command -Command { Get-WmiObject -Class win32_ComputerSystem } -Session ( ... + ~ + CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Command 2

Invoke-Command -Command { Get-WmiObject -Class win32_ComputerSystem } -Session $session[1]

this command works as i expected

can anyone explain why first one doesn’t work


I wasn’t able to reproduce that behavior; both calls to Invoke-Command worked for me. What output do you get if you run this command?

Get-PSSession -ComputerName localhost

Hi dave

i want the same result as command 2 as shown in the image