Run mstsc.exe with specified username and password

强颜欢笑 提交于 2019-11-27 18:10: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();

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

Sam Stephenson

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

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();

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.

egl35720
@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

The accepted answer solves the problem, but has the side effect of leaving the credentials in the users credential store. I wound up creating an IDisposable so I can use the credentials in a using statement.

using (new RDPCredentials(Host, UserPrincipalName, Password))
{
    /*Do the RDP work here*/
}

internal class RDPCredentials : IDisposable
{
    private string Host { get; }

    public RDPCredentials(string Host, string UserName, string Password)
    {
        var cmdkey = new Process
        {
            StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/list",
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };
        cmdkey.Start();
        cmdkey.WaitForExit();
        if (!cmdkey.StandardOutput.ReadToEnd().Contains($@"TERMSRV/{Host}"))
        {
            this.Host = Host;
            cmdkey = new Process
            {
                StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/generic:TERMSRV/{Host} /user:{UserName} /pass:{Password}",
                WindowStyle = ProcessWindowStyle.Hidden
            }
            };
            cmdkey.Start();
        }
    }

    public void Dispose()
    {
        if (Host != null)
        {
            var cmdkey = new Process
            {
                StartInfo =
            {
                FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\cmdkey.exe"),
                Arguments = $@"/delete:TERMSRV/{Host}",
                WindowStyle = ProcessWindowStyle.Hidden
            }
            };
            cmdkey.Start();
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!