如何通过VBScript向远程桌面(RDP)弹窗传入用户名和密码
Great question! Let's break this down clearly:
Short Answer
You can't directly auto-fill the RDP password field with pure VBScript—but there are reliable workarounds that integrate with VBScript to achieve your goal.
Why Direct VBScript Input Doesn't Work
The RDP password input box is a protected security control (guarded by Windows' security features to prevent credential theft). VBScript's built-in tools like SendKeys or WSH objects can't interact with this control directly—any attempt to send keystrokes to it will likely be blocked or fail silently.
Reliable Workaround: Use cmdkey to Save Credentials
The best approach is to pre-save your RDP credentials to Windows Credential Manager using the cmdkey command, then launch MSTSC via VBScript. This skips the login prompt entirely because Windows will automatically use the stored credentials.
Here's a complete VBScript example:
Dim WSHShell Set WshShell = WScript.CreateObject("WScript.Shell") ' Replace these values with your actual server, username, and password Dim serverName, userName, userPass serverName = "your-target-server" userName = "your-username" ' Use format like "domain\username" for domain accounts userPass = "your-password" ' Save credentials to Windows Credential Manager (runs silently) WshShell.Run "cmdkey /generic:TERMSRV/" & serverName & " /user:" & userName & " /pass:" & userPass, 0, True ' Launch RDP connection—no login prompt will appear WshShell.Run "MSTSC /v:" & serverName WScript.Quit
Notes for This Method:
- Credentials are stored persistently in Windows Credential Manager until you delete them with
cmdkey /delete:TERMSRV/your-target-server. - For domain environments, make sure your username includes the domain prefix (e.g.,
CORP\john.doe). - If you get permission errors, run the script as an administrator.
Unreliable (But Possible) SendKeys Hack
If you absolutely need a quick-and-dirty solution (not recommended for production or secure environments), you can try using SendKeys to simulate keystrokes. This is highly unreliable—it depends on the RDP window loading at exactly the right time, and the password is stored in plaintext.
Here's an example (use at your own risk):
Dim WSHShell Set WshShell = WScript.CreateObject("WScript.Shell") ' Launch RDP WshShell.Run "MSTSC /v:your-target-server" ' Wait for the window to load (adjust sleep time if needed) WScript.Sleep 3000 ' Send username WshShell.SendKeys "your-username" WScript.Sleep 500 WshShell.SendKeys "{TAB}" ' Move to password field WScript.Sleep 500 ' Send password (plaintext—extremely insecure!) WshShell.SendKeys "your-password" WScript.Sleep 500 WshShell.SendKeys "{ENTER}" ' Click "Connect" WScript.Quit
Why This Is a Bad Idea:
- If the RDP window takes longer to load than your sleep timer, keystrokes will send to whatever window is active (e.g., your desktop or a browser).
- Password is stored in plaintext in the script—anyone with access to the file can steal it.
- Windows security updates may break this functionality at any time.
Final Recommendation
Stick with the cmdkey method—it's secure, reliable, and supported by Windows natively. The SendKeys trick is only for temporary, low-security scenarios.
内容的提问来源于stack exchange,提问作者bala raju

