Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run mstsc.exe with specified username and password

Tags:

c#

rdp

I realize that in Windows 7, it is not possible to save different credentials for the same host, but I need some workaround.

Can I provide the username and password manually in the code? Store them in a temp .rdp file?

like image 711
Krzysiek Avatar asked Jul 02 '12 15:07

Krzysiek


People also ask

How do I allow credentials for RDP connection?

In the Local Group Policy Editor console go to the section Local Computer Policy > Computer Configuration > Administrative Templates > System > Credentials Delegation. Find the policy named “Allow delegating saved credentials with NTLM-only server authentication”.

How do I save mstsc credentials?

Hit Start –> Run and type "gpedit. msc". Navigate to Local Computer Policy –> Computer Configuration –> Administrative Templates –> System –> Credentials Delegation. Double click the policy "Allow Delegating Default Credentials with NTLM-only Server Authentication".

What is mstsc.exe admin?

MSTSC is the official command for Remote Desktop on Windows computers. The mstsc command can be used from the command prompt to perform special and specific tasks quickly along with parameters also called switches. Some switch example are: /v:computer: Specifies the remote computer to which you want to connect.


2 Answers

Process rdcProcess = new Process(); rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"); rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/192.168.0.217 /user:" + "username" +  " /pass:" + "password"; rdcProcess.Start();  rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe"); rdcProcess.StartInfo.Arguments = "/v " + "192.168.0.217"; // ip or name of computer to connect rdcProcess.Start(); 

The above code initiates a connection with .217 and I am not being prompted to provide a password. Thanks for help.

like image 183
Krzysiek Avatar answered Sep 28 '22 16:09

Krzysiek


If you want to use powershell you could add the credentials using

cmdkey /generic:DOMAIN/"computername or IP" /user:"username" /pass:"password" 

Then call RDP connection using

Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:computer name/IP" -Wait 

If you want to delete the credentials run

cmdkey /delete:DOMAIN/"Computer name or IP" 

Remember to remove ""

like image 43
Sam Stephenson Avatar answered Sep 28 '22 15:09

Sam Stephenson