7

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.

2
  • either create a service or a scheduled task to run your exe ? Commented Jul 18, 2013 at 16:13
  • Does it work if you start setup_server.exe via Start-Process (non-blocking for the ps session) instead of directly? Commented Jul 18, 2013 at 17:07

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank 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.
1

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.

3 Comments

# 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.
If 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
I do it in script, so shell gets closed.
1

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

0

In order to keep a powershell code running on the session exit it should be a process. And the windows way to keep the process is running a .exe or a windows service.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.