bala raju
bala raju

Reputation: 41

How to send username and password to the remote desktop (RDP) popup

Consider:

Dim WSHShell

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "MSTSC /v:servername"

WScript.Quit

I am able to open the RDP popup and give a server name to connect to. I need to provide the username, password, and click OK. Is there a way to achieve this from VBScript?

Upvotes: 2

Views: 8524

Answers (2)

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

You can use the following code:

Dim objShell, strMachineName, strUserName, strUserPwd
set objShell = createObject("wscript.shell")
strMachineName = "enter-machine-name"
strUserName = "enter-your-user-name"
strUserPwd = "enter-user-password"
objShell.Run "cmdkey /generic:"&strMachineName&" /user:"&strUserName&" /pass:"&strUserPwd
objShell.run "mstsc /v: "&strMachineName
set objShell = Nothing

Reference on cmdkey

I have tested this on Windows 7 and it works.

Upvotes: 4

Andrew Drake
Andrew Drake

Reputation: 665

I can suggest two options.

1: You can save an RDP connection (see picture below) and just run the new .rdp file from WshShell. After you create the .rdp file, you will have to log into it the first time, enter your credentials, and check the "Remember My Credentials" option.

Save RDP

2: You could use the SendKeys method. It is ugly but works. The downside is the password is left in the code, so you may want to look into encryption if you go this route. You may also need to tune a wait (sleep) for waiting for the popup to come up.

WScript.Sleep 5000 'Sleeps for 5 seconds
SendKeys “{TAB}”, 1 'Focus to the computer name
SendKeys "ServerName", 1
SendKeys "{TAB}", 1 'Focus to the user name
SendKeys "Password", 1
SendKeys "{ENTER}", 1 'Connect

Upvotes: 0

Related Questions