Disconnected PS-Session

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Windows LiveUser161 Windows LiveUser161 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #48501

    Hello, this is my first time on the forums and if this is something that was already answered i really do apologize.

    I've been looking into the ps-session functionality for automation of remote tasks and would like to issue commands into a remote-session without waiting for it to complete.

    I've found that i can use invoke-command with -InDisconnectedSession switch and -ComputerName argument to establish a new session on a remote computer and execute the script asynchronously.

    I am wondering how i might go about getting something similar but while reusing an existing session instead, something similar to:

    $s = new-pssession 'server.example.com'
    disconnect-pssession $s
    invoke-command -Session $s { $x = 10; }
    invoke-command -Session $s { write-host 'start first command with ' $x; [System.Threading.Thread]::Sleep(10000); write-host 'ok, im done'; }
    invoke-command -Session $s { write-host 'start second command with ' $x; [System.Threading.Thread]::Sleep(10000); write-host 'ok, im done'; }
    connect-pssession $s;

    This script above doens't work because i can't invoke on a disconnect session. I can only find articles on the web about using disconnected sessions when this invoke-command -ComputerName -InDisconnectedSession syntax is used.

    I'd like to be able to queue up a couple commands to run against a disconnected session and then connect later to review the outcome. It looks like the only way i could really use connect-pssession at this point is if i lose the session due to a network disruption or terminate it from a second process or something like that.

    Is there any realistic examples of using disconnect-pssession and is there any way to do what i want?

    #48506
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Hello,

    The PowerShell jobs feature might be worth looking into. You could submit all the commands you want to run on a list of remote server as one script block if you know the commands to execute in advance.

    A possible solution could be:

    Please let us know your thoughts and give above a try.

    Best,
    Daniel

    #48512

    Using Jobs would give me asynchronicity but it does not offer the shared enviroment / composition part that i'm needing / wanting from the session based approach:

    I'd like to share the same environment across multiple, distinct script blocks. Assuming that one script block sets a value of $x=10, using a session, that value would carry over to subsequent blocks. This has more important implications when loading larger data sets.

    I'd also like to be able to compose the scripts dynamically, if there was / is a simple addition operator for script blocks then i might be able to fathom a way for doing this with jobs but it is also seems a lot like pounding a round peg into a square hole this way.

    I really appreciate the feedback, but in short, I would still prefer to get this going with the session family of commandlets.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.