Using sessions with Invoke-Command

Welcome Forums General PowerShell Q&A Using sessions with Invoke-Command

This topic contains 2 replies, has 2 voices, and was last updated by

4 years, 11 months ago.

  • Author
  • #14309

    Points: 0
    Rank: Member

    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


  • #14311

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

    Hi dave

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


The topic ‘Using sessions with Invoke-Command’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort