Using sessions with Invoke-Command

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Kasun Rajapakse Kasun Rajapakse 3 years, 2 months ago.

  • Author
    Posts
  • #14309
    Profile photo of Kasun Rajapakse
    Kasun Rajapakse
    Participant

    Hi all

    i have come through a confusion between following two commands

    Command

    $session = New-PSSession -ComputerName KASUNRAJAPAKSE,localhost
    Get-PSSession

    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

    thanks

  • #14311
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
  • #14317
    Profile photo of Kasun Rajapakse
    Kasun Rajapakse
    Participant

    Hi dave

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

    thanks

You must be logged in to reply to this topic.