2

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bala raju
  • 41
  • 1
  • 3
  • Have a look at **RDP automation**.. although, strictly speaking, this shouldn't be allowed as most company policies strictly prohibit this approach due to security reasons – Zac Apr 03 '18 at 11:44
  • Short of automating RDP, you could potentially pop up the remote desktop UI and use SendKeys to have the script type username/tab/type password. That's cheesy, but effective. – JNevill Apr 03 '18 at 14:29

2 Answers2

4

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.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
0

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
Andrew Drake
  • 655
  • 1
  • 11
  • 25
  • How to click on "Ok" button in Legal notice after RDP login. i try using delay and sendkeys Tab , enter but not working. Can any one suggest. – bala raju Apr 06 '18 at 05:32
  • Are you talking about the "The identity of the computer cannot be verified. Do you want to connect anyway?" message? If so, you could either check the "Dont ask me again" box after first login, or you could SendKeys "{TAB}" three times then "{ENTER}". You may need to add a small pause before the sendkeys too – Andrew Drake Apr 06 '18 at 12:23
  • Or are you referring to a message IN the RDP session? If so I am not sure you can send keys to an RDP. See this previous post: https://stackoverflow.com/questions/1138606/alternative-to-sendkeys-when-running-over-remote-desktop – Andrew Drake Apr 06 '18 at 12:23