How to keep remote powershell command alive after session end?
Solution 1
The way to keep the remote PowerShell session running after the command has finished is to use a PSSession e.g.:
$s = new-PSSession computername
Invoke-Command -session $s { ..script.. }
... do other stuff, remote powershell.exe continues to run
Remove-PSSession $s # when you're done with the remote session
Generally though exes should run independently from the app that launched them.
Solution 2
Why are you using Invoke-Command. If you want a persistent Session, use Enter-PSSession.
$s = New-PSSession -Computername "Computername";
Enter-PSSession -Session $s;
setup_server.exe
# Once you are finnished
Exit-PSSession
With 'Enter-PSSession' you are not just Invoking some Command on the Server, you are directly logged-in like you probably know from SSH.
Solution 3
If you want your powershell session to keep running because you are running an exe, try using the -InDisconnectedSession switch. From what I understand, it will run the executable on the remote machine in a session that isn't actually connected to your computer. In essence, your computer will not destroy the session, when it disconnects, allowing the exe to continue to run.
invoke-command -computername RCOMPUTERNAME -scriptblock {start-process setup_server.exe} -InDisconnectedSession
If you need to do this on multiple computers. Setup an array of all the computer names.
Note: I don't believe this works with sessions that are already created.
Comments
-
Mihran Hovsepyan almost 2 years
I use the following command to run
setup_server.exe
on remote Windows box:powershell -command "$encpass=convertto-securestring -asplaintext RPASSWORD -force;$cred = New-Object System.Management.Automation.PSCredential -ArgumentList RUSER,$encpass; invoke-command -computername RCOMPUTERNAME -scriptblock {setup_server.exe} -credential $cred;"
setup_server.exe
's task is to create some configuration files and startmy_server.exe
(some daemon process), then it finishes. And I wantmy_server.exe
to keep running aftersetup_server.exe
is finished.So when I do it via CMD on local box (i.e. just run
setup_server.exe
from CMD) it works, but when I do it via powershell on remote host it doesn't work. Namely themy_server.exe
gets started, but right aftersetup_server.exe
is closed the server also gets closed(killed).So the question is following: Which powershell flags/cmdlets should I use to make the described scenario to work as in local mode?
NOTE: I want synchronously get output of
setup_server.exe
, so running remote command with-AsJob
flag, probably wouldn't work for me, though I even don't know if it will keep the server alive aftersetup_server.exe
's end. -
Mihran Hovsepyan almost 11 yearsThank you for your time and answer @Keith Hill, but it doesn't work. I've tried this:
powershell -command \"\$encpass=convertto-securestring -asplaintext mypassword -force;\$cred = New-Object System.Management.Automation.PSCredential -ArgumentList myuser,\$encpass;\$session = new-PSSession -computername HOSTNAME -credential \$cred; invoke-command -session \$session -scriptblock {setup_server.exe};\"
but again the server was killed right after setup_server is done. -
Mihran Hovsepyan almost 11 years
# Once you are finnished
this would not happen. I want to start remote server and that is all. Then I want to exit the session keeping the server running on remote host. -
algorhythm almost 11 yearsIf you don't close the shell, I think the session should remain open. At least for some time... With 'New-PSSessionOption -IdleTimeOut ??? -OperationTimeOut ??? -CancelTimeOut ??? -OpenTimeOut ???' you can setup the Timeouts: technet.microsoft.com/en-us/library/hh849703.aspx
-
Mihran Hovsepyan almost 11 yearsI do it in script, so shell gets closed.