Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep remote powershell command alive after session end?

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 start my_server.exe (some daemon process), then it finishes. And I want my_server.exe to keep running after setup_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 the my_server.exe gets started, but right after setup_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 after setup_server.exe's end.

like image 377
Mihran Hovsepyan Avatar asked Jul 18 '13 15:07

Mihran Hovsepyan


People also ask

How long does a PSSession last?

The idle time-out value of a session can also be changed when disconnecting from a session or reconnecting to a session. For more information, see Disconnect-PSSession and Connect-PSSession . In Windows PowerShell 2.0, the default value of the IdleTimeout parameter is 240000 (4 minutes).

How do I keep a PowerShell session active?

Right-click the script file and choose Run With PowerShell to run it (or copy it to the target Windows machine you wish to run it on). You will see the Windows PowerShell screen (Figure D). The script must remain running in order to work for you, but you can minimize this window.

How do I keep PowerShell open after script?

Using Read-Host to keep PowerShell console openUsing the PowerShell Read-Host command at the end of the script ensure console windows stay open until you press enter button to end the program.


3 Answers

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.

like image 26
algorhythm Avatar answered Sep 23 '22 05:09

algorhythm


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.

like image 23
Andy Avatar answered Sep 20 '22 05:09

Andy


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.

like image 69
Keith Hill Avatar answered Sep 23 '22 05:09

Keith Hill