Trying to do mstsc remotely using powershell doesn't work

466 views Asked by At

I have two server as X and Y. I have a powershell script that I am using for having a remote desktop of X on Y. The powershell script that I have to run on Y is --

  $hostname = 'X'
  $User = 'u-name'
  $Password = 'password'

        $ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo
        $Process = New-Object System.Diagnostics.Process

        $ProcessInfo.FileName = "$($env:SystemRoot)\system32\cmdkey.exe"
        $ProcessInfo.Arguments = "/generic:TERMSRV/$hostname /user:$User /pass:$Password"
        $Process.StartInfo = $ProcessInfo
        $Process.Start()

        $ProcessInfo.FileName = "$($env:SystemRoot)\system32\mstsc.exe"
        $ProcessInfo.Arguments = "$MstscArguments /v $hostname"
        $Process.StartInfo = $ProcessInfo
        $Process.Start()

When I run this script locally on Y, it does run and opens the server X in Y.

But I want to trigger it from X only to open X in Y. So I Invoke this powershell script from X as --

$pass = ConvertTo-SecureString -AsPlainText test-password -Force 
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList test-uname,$pass
$hostn = hostname
$Use = 'u-name'
$Pass = 'password'
Write-Host "$hostname"
$ScriptBlockContent={
Param ($hostname,$User,$Password)
E:\Script\test.ps1 $hostname $User $Password}
Invoke-Command -ComputerName Y  -Credential $cred -Scriptblock $ScriptBlockContent -ArgumentList $hostn,$Use,$Pass 

When I am invoking this. It does open mstsc.exe on Y but only for some fraction of seconds and doesn't open the server X on Y. Can somebody please help.. !!

Thanks.

1

There are 1 answers

3
briantist On

You are trying to launch an application with a GUI in a remote powershell session which has no desktop/display. Even if you use the credentials of a logged in user, the things you launch through Invoke-Command will not be visible to the logged-in user's session.

I think that this is possible with PsExec.exe, but I can't confirm.