通过制作.bat文件进行远程桌面连接

时间:2013-12-28 02:50:23

标签: batch-file remote-desktop

我想通过制作.bat文件将我的电脑连接到另一台电脑。当我运行该文件时,它应该连接到另一台PC。我写了“mstsc /v:192.168.15.102”命令,当我执行此文件时,它打开远程桌面窗口并要求用户名和密码。如何避免该窗口并在.bat文件中传递凭证。

3 个答案:

答案 0 :(得分:3)

我发现了这个

    cmdkey /generic:TERMSRV/$server /user:$user /pass:$Password
mstsc /v:$Server

这里 http://www.poshpete.com/powershell/passing-username-and-passwords-to-remote-desktop-rdp-automatically

但我认为这只适用于PowerShell,我对Windows没有经验。

在评论中我也看到了这个:

对于那些正在寻找这个想法但想要使用批处理的人,我创建了以下内容。从记事本中保存一个mstscup.cmd(不要忘记在最后删除.txt!) 从命令行调用它,如下所示: mstscup“servername”“user”“pass” 我不喜欢离开用户并传入密码保险库的想法,因此它会在120秒后清除它(保持命令提示符窗口打开)。 随意修改!

@echo off
setlocal
:: Check if the user passed at least 1 argument
if “%1%” == “” (
echo Remoted Desktop connection with user and password
echo.
echo Missing arguments. Syntax:
echo %~nx0% “servername” “username” “password”
echo.
echo Jean Morin, v0.1, 2013-02-23
pause
goto :eof
)
:: Next line removes surrounding quotes from server name
set sServer=%~1%
:: Keep the quotes for the username and password (in case spaces exists)
set sUser=%2%
set sPass=%3%
:: Seconds to wait before clearing the newly added password from the vault (see control panel, manage your credentials)
:: You may want to modify this if the server takes longer to connect (WAN). You could add this as a fourth argument.
set sSeconds=120
:: Add a new connection definition method to the vault
cmdkey /generic:TERMSRV/%sServer% /user:%sUser% /pass:%sPass%
:: Connect to the server as a new task
start mstsc /v:%sServer%
:: ping ourselves for x seconds (acts like a pause) then removes the newly added password from the vault
ping -n %sSeconds% 127.0.0.1 >nul:
cmdkey /delete:TERMSRV/%sServer%

答案 1 :(得分:1)

我能找到的最佳解决方案是保存rdp配置。 比这些选项要简单得多。 因此,通过开始菜单或win+r mstsc.exe打开“远程桌面连接”。 然后选择高级选项,更改所需的所有内容,然后另存为。 这将创建一个.rdp文件,可以单击该文件以运行或在命令提示符下运行。 :)

答案 2 :(得分:0)

通过双击使用Java和批处理文件进行远程登录

  1. 创建批处理文件Remote.bat并编写以下代码,

    @echo off
    java Remote DEV
    
  2. 创建一个Java文件Remote.java并编写以下代码, 还可以通过代码更改远程计算机的IP地址。

    import java.awt.MouseInfo;
    import java.awt.Robot;
    import java.awt.event.InputEvent;
    import java.awt.event.KeyEvent;
    import java.util.concurrent.TimeUnit; 
    
    public class Remote
    {
        public static void main(String args[])
        {
            try 
            {
                //showPosition();
    
                System.out.println("Remote Desktop for-->"+args[0]);
                String IP = "";
    
                if("DEV".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.41.101";
                }
                else if("UAT".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.43";
                }
                else if("PRE-PROD".equalsIgnoreCase(args[0]))
                {
                    IP = "mstsc /v:10.0.45.209";
                }
    
                Process p = Runtime. getRuntime(). exec(IP);
                Robot bot = new Robot();
                long mask = InputEvent.MOUSE_EVENT_MASK;
    
                TimeUnit.SECONDS.sleep((long) 2.5);
    
                bot.mouseMove(607, 290);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
                bot.keyPress(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_Y);
                bot.keyRelease(KeyEvent.VK_SHIFT);
                bot.keyPress(KeyEvent.VK_E);
                bot.keyPress(KeyEvent.VK_S);
                bot.keyPress(KeyEvent.VK_B);
                bot.keyPress(KeyEvent.VK_A);
                bot.keyPress(KeyEvent.VK_N);
                bot.keyPress(KeyEvent.VK_K);
                bot.keyPress(KeyEvent.VK_1);
    
                bot.mouseMove(765, 508);           
                bot.mousePress((int) mask);     
                bot.mouseRelease((int) mask);
    
            } 
            catch (Exception e) 
            {
                System.out.println("Exception send--->"+e.getMessage());
                e.printStackTrace();
            } 
        }
    
        public static void showPosition() throws InterruptedException
        {
            try
            {
                while(true == true)
                {
                    TimeUnit.SECONDS.sleep(1/2);
                    double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
                    double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
                    System.out.println("X:" + mouseX);
                    System.out.println("Y:" + mouseY);
                    //make sure to import 
                }
            }
            catch(Exception e)
            {
                System.out.println("Excpetion inside showPosition-->"+e.getMessage());
            }
        }
    
    
    }
    

现在保存代码,然后双击Remote.bat。 它将自动打开您的远程计算机。 Enjoyyyyyyy