Pass argument to PSSession?

Welcome Forums General PowerShell Q&A Pass argument to PSSession?

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

1 year ago.

  • Author
  • #12420

    Points: 0
    Rank: Member

    I know I can use argumentlists, or $using for Invoke-Command for executing scriptblocks remotely, however I was wondering if there was any way to pass an argument into an interactive pssession with Enter-PSSession or New-PSSession ?

  • #12444

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Well, those are two different questions.

    You can't "pass arguments into a session." A session is a runspace on a machine; what you can certainly do is run a command in that runspace. A session isn't running anything implicitly, so there's no need for arguments.

    $s = New-PSSession -computername SERVER
    Invoke-Command -Session $s -scriptblock { $var = 5 }

    The remote session now has $var equal to 5.

    WRT Enter-PSSession, it's the same story. You're just interacting with a runspace. If you want to create a variable in that runspace, just do so. But the command doesn't offer a way to pre-pass anything into the runspace, no. That isn't really what sessions are for.

    Invoke-Command is different because it's actually running a command, so there's an obvious need for arguments.

  • #93109

    Points: 0
    Rank: Member

    Rather than explicitly setting $var to 5 how do you set $var to a variable that was set before executing the New-PSSession?

    • #93112

      Points: 741
      Helping Hand
      Rank: Major Contributor

      Please do not reactivate old posts.

      Please read the complete help for Invoke-Command to learn how to use local variables in script blocks. I specially recommend the example #9! 😉

The topic ‘Pass argument to PSSession?’ is closed to new replies.

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