Remote Desktop Connection by making .bat file

2020-05-29 08:40发布

问题:

I want to connect my pc to another pc by making .bat file. When i run that file it should connect to the other pc. I wrote "mstsc /v:192.168.15.102" command when i execute this file it opens remote desktop window and demands username and password. How can i avoid that window and pass the credential in .bat file.

回答1:

I found this

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

from archive (or original)

But I think that's for powershell only and I'm inexperienced with windows.

Along the comments I also saw this one:

For people that were looking for this idea but want to use batch instead I created the following. Save this a mstscup.cmd from notepad (don’t forget to remove the .txt at the end!) Call it from the command line as follows: mstscup “servername” “user” “pass” I didn’t like the idea of leaving the user and pass in the password vault so it clears it after 120 seconds (leaves a command prompt window opened). Feel free to modify at will!

@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%


回答2:

Remote Login using java and batch file by double click

  1. Create batch file Remote.bat and write the following code,

    @echo off
    java Remote DEV
    
  2. Create a java file Remote.java and write following code, also change the IP address of your remote computer in code.

    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());
            }
        }
    
    
    }
    

Now save the code and double click on Remote.bat. it will automatically open your remote computer. Enjoyyyyyyy