Run mstsc.exe with specified username and password

101,435

Solution 1

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.

Solution 2

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 ""

Solution 3

This is an updated version from Krzysiek's post.

var rdcProcess = new Process
    {
        StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = String.Format(@"/generic:TERMSRV/{0} /user:{1} /pass:{2}", 
                            fp.ipAddress,
                            (String.IsNullOrEmpty(fp.accountDomain)) ? fp.accountUserName : fp.accountDomain + "\\" + fp.accountUserName,
                            fp.accountPassword),
                            WindowStyle = ProcessWindowStyle.Hidden                                
            }
    };
rdcProcess.Start();
rdcProcess.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\mstsc.exe");
rdcProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
rdcProcess.StartInfo.Arguments = String.Format("/f /v {0}", fp.ipAddress); // ip or name of computer to connect
rdcProcess.Start();

Solution 4

While trying to figure out how to allow users into our network, without giving them the keys to the castle, I enabled Remote Desktop Access for a few members of my team. Thinking more about this, I quickly remembered a project several years ago while working for the Department of Defense. That project required us to "lock down" access to only necessary personnel and limited access to the programs on the servers. After spending some time on Microsoft's KnowledgeBase, we realized that we could create desktop "shortcuts" for those employees that made the RDP connection, logged them in and limited their access to one specific application on that server.

Solution 5

@echo off

cmdkey /generic:TERMSRV/"*IP or Server Name*" /user:%username%

start mstsc /v:*IP or Server Name*

cmdkey /delete:TERMSRV/"*IP or Server Name*"

quit
Share:
101,435
Krzysiek
Author by

Krzysiek

Updated on September 30, 2020

Comments

  • Krzysiek
    Krzysiek over 3 years

    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?