Reputation: 11
In Windows Powershell (admin):
$session = New-PSSession -ComputerName "xx.xx.xx.xx"
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.
Upvotes: 1
Views: 171
Reputation: 437753
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
winrs -r:yourcomputer cmd
)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.
Upvotes: 2