0

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 Answer 1

1

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.

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

3 Comments

Thanks. I tried -- "E:\PS tool\PsExec.exe" -i Y -u test-uname -p test-password E:\Script\test.cmd X u-name password It also didn't work.
test.cmd is calling the same powershell script.
Try E:\PS tool\PsExec.exe \\Y -i -u test-uname -p test-password E:\Script\test.cmd X u-name password.

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.