Search code examples
powershellwindows-10hyper-vinvoke-command

Programs running on Hyper-V with Invoke-Command hang


I'm trying to run my software on Hyper-V VM using powershell Invoke-Command, without success. Host OS -Windows 10. Guest OS - also Windows 10. VM Generation 1.

I did some simple experiments and found out this:

If I run

Invoke-Command -VMName MY_VM -Credential $Cred -ScriptBlock { calc }

then I can see launched calc.exe on the guest system right with UI.

But if I run mspaint or any non-Microsoft program, nothing happens. The program just hangs in the VM TaskManager without any effect.

I also tried to run several different programs using CLI calling Invoke-Command several ways, but got the same result.

What could be the problem?


Solution

  • The basic answer is that powershell remote connections (or any remote connection like rdp, ssh, etc) take place in a separate logon session, and can't really interact with each other.

    There are two reasonable ways to do this:

    1. Use PsExec - part of the microsoft sysinternals tools group.
    # List sessions - note the session ID of the session you want the process to start in
    quser /server:$computername
    # Run a process remotely, specifying the logon ID
    PsExec.exe -s -i $ID notepad.exe
    
    1. Use a scheduled task that runs when you are logged in and is visible. You can do this with powershell's various New-ScheduledTask commands to create one, or follow this guide by Scripting Guy! using WMI Win32_ScheduledJob methods.

    See use powershell to start a gui program on a remote machine for more details on both options, and a well-written description of why it's hard to do in windows.