使用指定的用户名和密码运行mstsc.exe

Krz*_*iek 27 c# rdp

我意识到在Windows 7中,无法为同一主机保存不同的凭据,但我需要一些解决方法.

我可以在代码中手动提供用户名和密码吗?将它们存储在临时.rdp文件中?

Krz*_*iek 39

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();
Run Code Online (Sandbox Code Playgroud)

上面的代码启动与.217的连接,我没有被提示提供密码.感谢帮助.

  • 如果您不想将存储的凭据保存在存储库中,则在此代码之后,您可以使用参数`/ delete:TERMSRV/192.168.0.217再次调用`cmdkey.exe` (2认同)

Sam*_*son 36

如果要使用powershell,可以使用添加凭据

cmdkey /generic:DOMAIN/"computername or IP" /user:"username" /pass:"password"
Run Code Online (Sandbox Code Playgroud)

然后使用调用RDP连接

Start-Process -FilePath "$env:windir\system32\mstsc.exe" -ArgumentList "/v:computer name/IP" -Wait
Run Code Online (Sandbox Code Playgroud)

如果要删除运行的凭据

cmdkey /delete:DOMAIN/"Computer name or IP"
Run Code Online (Sandbox Code Playgroud)

记得删除""


Spe*_*cer 9

这是Krzysiek的帖子的更新版本.

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();
Run Code Online (Sandbox Code Playgroud)

  • @MarekBar 您的域可能有一个 GPO,禁止存储远程桌面连接的密码。 (2认同)