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?