1

In Windows Powershell (admin):

Connect to Remote Windows

$session = New-PSSession -ComputerName "xx.xx.xx.xx"

Attempting Remote Execution

Invoke-Command -Session $session -ScriptBlock {
            cd C:\Path
            ls
            Start-Process -FilePath "C:\Path\MyProgram.exe"
            start-Process cmd
        }

The cd and ls commands work as expected, but the Start-Process cmd and MyProgram.exe do not launch. No windows appear; it seems to be skipped.

I've also tried another approach:

PS C:\Jenkins> Enter-PSSession -Session $session
[xx.xx.xx.xx]: PS C:\Users\Administrator\Documents> Start-Process cmd
[xx.xx.xx.xx]: PS C:\Users\Administrator\Documents> (nothing happens)

Both way failed to launch or execute a new window or .exe file.

I tried to execute commands remotely and expected new windows or applications to be launched, but this did not happen.

4
  • I have tried this , but still not work Commented Oct 27, 2023 at 10:23
  • It sounds like everything is working, and, assuming the file path is correct, I'm concerned about the fact that Start-Process creates a new process. Is there a reason you a cannot use -NoNewWindow? And if you can use -NoNewWindow, what happens? Commented Oct 27, 2023 at 11:29
  • To explain further, I'm concerned that the current session has its own process, and any new process probably isn't part of the current session. This is how things seem to work elsewhere, and may be what is happening here. Commented Oct 27, 2023 at 11:57
  • 1
    @Darin, it isn't creation of child processes per se that's the problem: any call to an external program requires one. While -Wait -NoNewWindow is a poor substitute for direct invocation locally, in remote invocations the -NoNewWindow causes the child process' output not to be shown - direct invocation works in principle in remote session (synchronously, in the same window, with standard streams connected to PowerShell's streams), but running interactive console applications (e.g. cmd.exe without /C) is fundamentally unsupported. Commented Oct 27, 2023 at 12:48

1 Answer 1

2
  • Fundamentally - whether remotely or not - there's rarely a good reason to use Start-Process to invoke external programs, especially if they're console applications.

    • To synchronously invoke the latter in the same window, with their standard streams connected to PowerShell's streams, invoke them directly (e.g. & "C:\Path\MyProgram.exe")

    • For guidance on when Start-Process is and isn't appropriate, see GitHub docs issue #6239

  • In the context of PowerShell remoting, there are additional pitfalls:

    • Given that Start-Process creates a new window by default, that window won't be visible in a remoting session.

    • Additionally, execution is asynchronous by default (also in local calls).

    • While you could make the invocation synchronous with -Wait, an attempt to also run in the same window with -NoNewWindow does not work in remoting: you won't see any output (in local calls, you'll see the output, but you won't be able to capture it; that's why direct invocation is preferable).

  • Finally, PowerShell's remoting does not support interactive console applications.


The upshot is:

  • Assuming "C:\Path\MyProgram.exe" is a console application, use direct invocation in the remote session (&, the call operator, is needed for syntactic reasons, because your executable path is quoted):

    & "C:\Path\MyProgram.exe"
    
  • However, your attempt to run cmd without /C and a specific command, i.e. your attempt to start an interactive session cannot work (the cmd.exe process starts up and quietly exits right away).


Options for running interactive console applications remotely:

  • You can use Windows Remote Management via the built-in winrs.exe CLI, which supports interactive console applications. Windows Remote Management uses the same infrastructure (WSMan/WinRM) as PowerShell remoting, so chances are it will work for targeting computers where PowerShell remoting is enabled; see the docs for setup information.

    # Enter an interactive session on computer "yourcomputer"
    winrs -r:yourcomputer cmd
    
    • Note:
      • You could even start an interactive PowerShell session this way (winrs -r:yourcomputer cmd)
      • Whatever executable (plus arguments, if needed) you pass to winrs.exe is implicitly executed via (cmd /c).
  • Alternatively, you can download and use psexec, which also supports interactive console applications, but uses a different remoting mechanism. It has many advanced features.

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

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.