Pass argument to PSSession?

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 3 years, 3 months ago.

  • Author
    Posts
  • #12420
    Profile photo of Kitt Holland
    Kitt Holland
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

You must be logged in to reply to this topic.