You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Excel启动RDP并传递主机IP地址与登录密码?

Launch RDP with Host IP and Password from Excel

Great question! The built-in mstsc.exe (Remote Desktop Connection) doesn't support passing a password directly via command line parameters—this is an intentional security restriction. But we can work around this by first storing the target host's credentials using Windows' native cmdkey utility, then launching the RDP session as usual.

Here's a modified version of your VBA code that pulls the host IP from the active cell and the password from the adjacent cell to the right:

Sub OpenRDPWithCredentials()
    Dim hostIP As String
    Dim password As String
    Dim cmdkeyCommand As String
    Dim mstscCommand As String
    
    ' Grab host IP from active cell, password from the cell immediately to its right
    hostIP = Trim(ActiveCell.Value)
    password = Trim(ActiveCell.Offset(0, 1).Value)
    
    ' Make sure we have both values before proceeding
    If hostIP = "" Or password = "" Then
        MsgBox "Oops! Please fill in both the host IP and password first.", vbExclamation
        Exit Sub
    End If
    
    ' Command to save credentials to Windows Credential Manager
    cmdkeyCommand = "cmdkey /generic:TERMSRV/" & hostIP & " /user:" & hostIP & " /pass:" & password
    
    ' Run cmdkey in the background (no visible command prompt)
    Shell cmdkeyCommand, vbHide
    
    ' Give a quick second for the credentials to save before launching RDP
    Application.Wait Now + TimeValue("00:00:01")
    
    ' Launch the RDP session
    mstscCommand = "c:\windows\system32\mstsc.exe /v:" & hostIP
    Shell mstscCommand, 1
End Sub

Important Details to Know:

  • Credential Storage: The cmdkey command saves your credentials in the Windows Credential Manager under the entry TERMSRV/[hostIP]. These credentials will stay there until you manually delete them (via Control Panel > Credential Manager) or overwrite them with a new password for the same host.
  • Security Note: If you're sharing this Excel file, keep in mind that anyone with access to the VBA editor can view the password logic. Consider adding password protection to your VBA project if you need to keep the code private.
  • Wait Time: The 1-second wait ensures the credentials are fully saved before RDP tries to connect—this prevents race conditions where the session might start before the credentials are ready.

Optional: Clean Up Credentials After Session (Simplified)

If you want to remove the stored credentials after the RDP session closes, you can add a cleanup step. Note that this uses a fixed wait time (adjust it based on how long users typically keep RDP open):

' Add this after launching the RDP session
' Wait 1 minute (adjust the time as needed)
Application.Wait Now + TimeValue("00:01:00")
' Delete the stored credentials
Shell "cmdkey /delete:TERMSRV/" & hostIP, vbHide

For a more reliable solution (instead of a fixed wait), you'd need to use Windows API functions to monitor the mstsc.exe process and wait for it to exit—but that adds more complexity. For most use cases, the simplified wait works fine.

内容的提问来源于stack exchange,提问作者Volkan

火山引擎 最新活动