Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file for remote desktop  (Read 56886 times)

0 Members and 1 Guest are viewing this topic.

ryank44

    Topic Starter


    Rookie

    Batch file for remote desktop
    « on: August 21, 2008, 10:23:51 AM »
    Hey,

    I was wondering if it is possible to create a batch file that will read ip addresses from a file, ie excel, and log on to remote desktop using them.
    Basically I want to log into about 30 PCs at the same time, using all different IP addresses.

    I have created a RDP with my settings and so on, but cannot figure out how to get it to 'fetch' the IP addresses, and then repeat.

    I'm thinking I will have to do this in C+ or something, but was hoping I could do it as a batch file.

    Any help would be appreciated.

    ryank44

      Topic Starter


      Rookie

      Re: Batch file for remote desktop
      « Reply #1 on: August 21, 2008, 12:03:37 PM »
      Ok,  I have done some research, and I made the batch file. What I need to do now, is have it confirm a question that basically gives some information and then asks you to click OK.

      is there a way I can have the batch file "click" ok for me?

      thank you.

      Carbon Dudeoxide

      • Global Moderator

      • Mastermind
      • Thanked: 169
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Experience: Guru
      • OS: Mac OS
      Re: Batch file for remote desktop
      « Reply #2 on: August 21, 2008, 08:25:54 PM »
      Quote
      is there a way I can have the batch file "click" ok for me?
      Batch files cannot Send Keys, sorry.

      ryank44

        Topic Starter


        Rookie

        Re: Batch file for remote desktop
        « Reply #3 on: August 22, 2008, 02:11:34 PM »
        UPDATE -

        I have now created a VB script within my batch file to add the {RETURN} I need, but it does not activate in the remote desktop logon.

        anyone know how to do this?
        my scripts look like this -

        batch file:

        for /f "delims=" %%T in (filename.txt) do (
        start mstsc /v: %%T /w:640 /h:480
        start d:\user\Desktop\sendkeys.vbs
        pause
        )

        and my .vbs file looks like this:

        Set objShell = CreateObject("WScript.Shell")
        WScript.sleep 2000
        objShell.SendKeys "{ENTER}"

        now, if i run the .vbs, then click on an application such as notepad, the enter will be pressed there, but i cant seem to get it to click in the window for the remote desktop.

        can anyone help me with this?

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Batch file for remote desktop
        « Reply #4 on: August 22, 2008, 04:00:06 PM »
        You would need to grab the Window to direct the sendkeys; you should be able to use VBScript for the entire sequence and eliminate the batch file.

        Code: [Select]
        Set objShell = CreateObject("WScript.Shell")
        objShell.Run "mstsc /v: %%T /w:640 /h:480"
        WScript.Sleep 500
        objShell.AppActivate "Remote Desktop Connection"
        WScript.Sleep 100
        objShell.SendKeys "{ENTER}"

        You may have to play around with the sleep timings.  How do you get the computer name into the window? Double check the Window title on your machine, I used the one on mine!

         8)
        « Last Edit: August 22, 2008, 04:38:23 PM by Sidewinder »
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        ryank44

          Topic Starter


          Rookie

          Re: Batch file for remote desktop
          « Reply #5 on: August 25, 2008, 06:18:13 AM »
          Thanks for the reply sidewinder, however it still is not sending the keystroke into the remote desktop.

          I got it to activate that window, but nothing happens when the window is up, ie no keys are being pressed.

          Any other suggestions? I read somewhere that I may need to use PCexec or something to be able to do this, but I would really like to avoid this.

          thanks

          ps - as per the window name, in the batch file I created, I ran the for loop to get the PC names from a file, and then opened them each individually. you can see in my previous post for the batch file code.

          What I am doing is checking 30 or so remote computers at a time of 1500 computers by computer name, so I want to open the remote connections to them automatically, but each has a screen that loads up and asks you to press 'ok' before they will load their desktops (and before the logon screen). I have the batch file successfully loading up 30 pcs from the list, but they all stop on that screen, and I have to manually go to each one and press enter for them to load up, which is too time consuming.
          I'm looking for a  way to bypass the initial screen, ie press enter or click Ok or whatever, but cant get any sendkeys to activate in the remote desktop.

          SEBNN



            Rookie

            Re: Batch file for remote desktop
            « Reply #6 on: August 25, 2008, 09:47:55 AM »
            Would it work to create a system variable that contains the characters for a carriage return and then call up this variable from your VB or batch file?

            ryank44

              Topic Starter


              Rookie

              Re: Batch file for remote desktop
              « Reply #7 on: August 25, 2008, 10:16:32 AM »
              Would it work to create a system variable that contains the characters for a carriage return and then call up this variable from your VB or batch file?

              I'm not sure.. I do not know how to do that, could you give me an example?

              thanks

              SEBNN



                Rookie

                Re: Batch file for remote desktop
                « Reply #8 on: August 25, 2008, 11:03:32 AM »
                Would it work to create a system variable that contains the characters for a carriage return and then call up this variable from your VB or batch file?

                I'm not sure.. I do not know how to do that, could you give me an example?

                thanks


                Its been a while, but I made an environmental variable called "CR" and then set it equal to the Char 10 I believe it was (the square).  I believe it is Char 10 and Char 13 used in a carriage return (enter), one means next line and one means back to the left margin.  I wish I could be more help, but like I said, it has been a while.

                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: Batch file for remote desktop
                « Reply #9 on: August 25, 2008, 11:39:18 AM »
                I'm a bit confused. The /v switch needs to point to a rdp file. If in fact, filename.txt has a list of computer names/ip addresses, this little snippet should work:

                Code: [Select]
                Const ForReading = 1

                Set objShell = CreateObject("WScript.Shell")
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set f = fso.OpenTextFile("filename.rdp", ForReading)

                Do Until f.AtEndOfStream
                strFile = f.ReadLine()
                objShell.Run "mstsc /w:640 /h:480"
                WScript.Sleep 500
                objShell.AppActivate "Remote Desktop Connection"
                WScript.Sleep 100
                objShell.SendKeys strFile
                WScript.Sleep 100
                objShell.SendKeys "{TAB}"
                WScript.Sleep 100
                objShell.SendKeys "{ENTER}"
                Loop
                f.Close

                The sendkeys method mimics what a user can do at the keyboard. This link describes what a rdp file is.

                Note: VBScript can create an Excel object. You can read the lsit of computer names direct from a spreadsheet.

                Note: If there is an error with the connect, the script will be out of sync with the window. Are you sure you want 30 active remote desktops? If you want to do one at a time, we can change the parameters on the run method.

                 8)

                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                ryank44

                  Topic Starter


                  Rookie

                  Re: Batch file for remote desktop
                  « Reply #10 on: August 26, 2008, 12:09:30 PM »
                  Thanks for your help Sidewinder, however it is still not working...

                  your code isn't seeming to work for me, but if you could do me a favor and just write a single code to open up notepad on the already connected remote computer, and then have it press enter in there that would be very helpful, as I believe I would be able to edit it to work for my computers.

                  I tried running the basic code for throwing  a key in the remote desktop, but it will still not work. your appactivate command works great for activating my remote desktop, as i have to use it as:
                  objShell.AppActivate "(compute name) - remote desktop"

                  and it does grab that window, but the key presses are still not activating for the remote desktop.

                  I'm starting to think that you cannot press keys in a remote desktop with basic VB script, and i may need to do this with an add-on.. but hopefully not.


                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Batch file for remote desktop
                  « Reply #11 on: August 26, 2008, 02:28:41 PM »
                  Something appears lost in translation. The posted code was for processing the mstsc window, not the remote desktop.

                  Code: [Select]
                  Set objShell = CreateObject("WScript.Shell")
                  objShell.Run "mstsc /w:640 /h:480"
                  WScript.Sleep 2000
                  objShell.AppActivate "Remote Desktop Connection"
                  WScript.Sleep 100
                  objShell.Sendkeys "computername"
                  WScript.Sleep 100
                  objShell.Sendkeys "{TAB}"
                  WScript.Sleep 100
                  objShell.SendKeys "{ENTER}"

                  You'd do the same for the remote desktop window, grab the window title with the AppActivate method, determine what object on the desktop has the focus, and use the tab key to navigate where you need to go. Sendkeys is primitive and when dealing with the internet, even the best planned timings between keytrokes can get messy.

                  Why are you using remote desktop? Are these computers networked? What are you trying to do? There might be an easier approach.

                   8)
                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein

                  ryank44

                    Topic Starter


                    Rookie

                    Re: Batch file for remote desktop
                    « Reply #12 on: August 27, 2008, 06:19:03 AM »
                    Quote
                    Why are you using remote desktop? Are these computers networked? What are you trying to do? There might be an easier approach.

                    What I am doing is this - I have to Audit 1500 networked PCs via Remote Desktop using the usernames. What I do is Log in to 30 or so, by manually opening 30 remote desktop connections, copying and pasting in the computer names into the RDP gui, and then after I connect, a screen prompts me to click 'OK' to a pop up window before promting for my user name and password. I have the username and password already entered, so I need to bypass this 'ok' screen so the computer will start loading for me, so when the last of the 30 is done logging in, the first computer will be totally loaded up.

                    What I have done now, is written a batch file to auto read my computer names from a file, and open up RDP.
                    The problem is I still have to Click 'ok', or press enter/space before the computer logs me in and begins to load up the computer.

                    So when I run my batch file, it loads up 30 remote connections to 30 different computers very fast, but they all stay at that same 'OK' screen, so I then have to go to each one, and click OK, and then wait for them all to load up. This is what I would like to bypass.

                    I have tried entering a VBS script for sending a key into my batch file to run with the loop for opening the batch files, but I cant even get a sendkey to work in an already connected and open remote desktop connection. In using your VBS, I was able to activate the remote desktop connection (its called " computername - Remote Desktop" but the rest of the sendkey script does nothing, ie does not activate <CR>.

                    If I change your script to activate notepad instead of Remote desktop, then the key is pressed. This is why I'm thinking RDP will not allow sendkeys to activate inside the remote connection.

                    Maybe this will make it a bit more clear, any help is appreciated. If there is some script that will just press enter inside remote desktop, then I'm sure I could implement it into either my batch file or a VBS script.

                    PS I just tested your script below, and it works really well for opening one remote connection after inputting the username into the script, BUT after your script ended, I added another sleep and sendkey enter, but it does not carry into my remote desktop connection.
                    I used the followint script:
                    Code: [Select]
                    Set objShell = CreateObject("WScript.Shell")
                    objShell.Run "mstsc /w:640 /h:480"
                    WScript.Sleep 2000
                    objShell.AppActivate "Remote Desktop Connection"
                    WScript.Sleep 100
                    objShell.Sendkeys "computer 1"
                    WScript.Sleep 100
                    objShell.Sendkeys "{TAB}"
                    WScript.Sleep 100
                    objShell.SendKeys "{ENTER}"
                    WScript.Sleep 10000
                    objShell.SendKeys "{ENTER}"
                    « Last Edit: August 27, 2008, 06:36:03 AM by ryank44 »

                    Sidewinder



                      Guru

                      Thanked: 139
                    • Experience: Familiar
                    • OS: Windows 10
                    Re: Batch file for remote desktop
                    « Reply #13 on: August 27, 2008, 07:06:37 AM »
                    Quote
                    So when I run my batch file, it loads up 30 remote connections to 30 different computers very fast, but they all stay at that same 'OK' screen, so I then have to go to each one, and click OK, and then wait for them all to load up. This is what I would like to bypass.

                    This window that has the OK button, does it have a title? This would be the window you want to grab; if the OK button already has focus, you can use sendkeys to mimic the ENTER key.

                    Quote
                    If I change your script to activate notepad instead of Remote desktop, then the key is pressed. This is why I'm thinking RDP will not allow sendkeys to activate inside the remote connection.

                    The sendkeys script mimics what a user would do at the keyboard (no cheating with the mouse). With the script running external to the application, RDP (or any other application) has no clue whether the key strokes are from a human or a script.

                    Quote
                    What I am doing is this - I have to Audit 1500 networked PCs via Remote Desktop using the usernames

                    Once connected, how do you do the audit? VBScript has a WshController object that allows for remote script execution. There is also a management interface to pull information about remote systems.

                     8)
                    The true sign of intelligence is not knowledge but imagination.

                    -- Albert Einstein

                    ryank44

                      Topic Starter


                      Rookie

                      Re: Batch file for remote desktop
                      « Reply #14 on: August 28, 2008, 09:43:05 AM »
                      Hey,

                      I have confirmed that the Sendkeys does not work for remote desktop.. no matter what I have tried.

                      I made an EXTREMELY simple VBS script to activate recycle bin, and then press enter. When i have remote desktop open, it will not grab the bin from there.. this is the code
                      Code: [Select]
                      Set objShell = CreateObject("WScript.Shell")
                      WScript.Sleep 2000
                      objShell.AppActivate "Computer 1 - Remote Desktop"
                      WScript.Sleep 500
                      objShell.SendKeys "{ENTER}"
                      WScript.Sleep 1000
                      objShell.AppActivate "recycle bin"
                      WScript.Sleep 5000
                      objShell.SendKeys "{ENTER}"

                      It is basically your code but simplified. I run it when the Computer 1 - Remote Desktop is already connected and minimized. It will pop the remote desktop up, but will not press any keys to activate on the other computer...

                      Do I need to edit the sendkeys so that the remote PC knows that they are for it, not for me?like
                      Code: [Select]
                      objShell.SendKeys.Computer 1 - Remote Desktop "{ENTER}"
                      or something to that effect?

                      Sidewinder



                        Guru

                        Thanked: 139
                      • Experience: Familiar
                      • OS: Windows 10
                      Re: Batch file for remote desktop
                      « Reply #15 on: August 28, 2008, 02:07:44 PM »
                      From reading on the web, remote desktop does not accept sendkeys. Have you got to the point where you can start RDP, and click on the OK button? That may be as far as you can go with RDP using automation.

                      Sendkeys does not support the Windows button, so something as simple as bringing up the run box (win-R) would be a chore. Sendkeys is fine in a small controlled environment, but on a larger scale can be a nightmare (think: 1500 machines!).

                      Manually, how do you do the audit? Do you have programs/scripts to run? Do you extract system information? Is the audit automated? If not can it be?

                      As I mentioned, there are ways to run scripts on remote machines, either with the WshController object or the newer Windows Remote Management protocol.

                      Sometimes it's easier to start from where you want to be and work backwards. 8)
                      The true sign of intelligence is not knowledge but imagination.

                      -- Albert Einstein

                      ryank44

                        Topic Starter


                        Rookie

                        Re: Batch file for remote desktop
                        « Reply #16 on: August 29, 2008, 10:15:55 AM »
                        Quote
                        From reading on the web, remote desktop does not accept sendkeys. Have you got to the point where you can start RDP, and click on the OK button? That may be as far as you can go with RDP using automation.

                        yeah, I have gotten that far.. I'm thinking that my only chance is to insert a Macro file, but I do not want to download a macro editor.. so was hoping to be able to do it in VB.

                        Quote
                        Manually, how do you do the audit? Do you have programs/scripts to run? Do you extract system information? Is the audit automated? If not can it be?

                        For one PC, the audit is as follows - connect with RDP - press enter at security screen - (com loads up as username/pw is auto, so wait for com to load) - go to system info, check current version, make sure there are no errors - log out.

                        So really, my auto login system is pretty fast, as I have it to remote into 30 PCs at a time, I can audit about 100 per hour.

                        My current batch File is working great, I was just hoping to be able to make it a bit more efficient.

                        Code: [Select]
                        for /f "delims=" %%T in (computer name.txt) do (
                        start mstsc /v: %%T /w:800 /h:600
                        pause
                        )

                        I just press enter for the security screen, then click back on the cmd window, press enter, repeat. It actually is pretty fast.

                        Thank you for your help tho, if I find some solution, I will post it here

                        devaraju

                        • Guest
                        Re: Batch file for remote desktop
                        « Reply #17 on: September 29, 2008, 02:06:51 AM »
                        hi team,

                        i have an similar issue with me daily i have to login to 12-15 windows servers and have to take report my desktop has winxp installed and the remote servers have win2k or win2k3 installed.

                        my question to u people is can i write a dos script which reads the servers name and opens rdp of 12-15 servers at a time if so pls send me the script

                        Thanks in advance.
                        raju ???

                        bakersboy



                          Newbie

                          Re: Batch file for remote desktop
                          « Reply #18 on: October 05, 2008, 10:33:52 PM »
                          Try 1/7 *.* loop it

                          raemshmshetty



                            Greenhorn

                            Re: Batch file for remote desktop
                            « Reply #19 on: October 06, 2008, 04:06:23 AM »
                            Dear Friends

                            I would like to write a windows bath file for the following

                            to open remote desktop connection (mstsc)
                            provide computer name  " computername"
                            provide user name " user name"
                            provide user password " password"
                            provide domain name "domain name"

                            to start the programme
                            program path and file name "d:\notepad.txt"
                            start in folder "d:\ "

                            Please help me out

                            I want to control my remote user to access only a particular programme not
                            any other folder  or even desktop
                            also tel me if there is any easiest alternative method


                            thanks in advance


                            ramesh shetty

                            Sidewinder



                              Guru

                              Thanked: 139
                            • Experience: Familiar
                            • OS: Windows 10
                            Re: Batch file for remote desktop
                            « Reply #20 on: October 06, 2008, 04:44:47 AM »
                            Raemshmshetty and Devaraju,

                            Not only is it considered rude to hijack another persons thread, but many responses are customized for each situation and may not be suitable for yours.

                            No matter how similar, please post your questions in a new thread.

                             8)
                            The true sign of intelligence is not knowledge but imagination.

                            -- Albert Einstein

                            ryank44

                              Topic Starter


                              Rookie

                              Re: Batch file for remote desktop
                              « Reply #21 on: April 16, 2009, 01:48:53 PM »
                              Ok, I have another Question on this script, so I figured I could post it back on here.

                              What I need to do now is have my batch file return a true or false, if it can/cannot connect.

                              Does anyone know of any method of doing this? See above for my 'for' loop, which is where I need to add it.

                              More or less I am logging into a hundred PCs at a time or so, and need a result of whether I was able to connect remotely without going one by one or going thru a huge list.

                              Thanks for the help, feel free to ask for additional info.

                              coppaj



                                Newbie

                                • Experience: Beginner
                                • OS: Unknown
                                Re: Batch file for remote desktop
                                « Reply #22 on: May 25, 2011, 07:53:00 AM »
                                We made a simple batch file to ask you a computer name and connect via RDP - here is our code.  Maybe you can modify for your use:

                                TITLE Brett's Remote Connection Tool
                                mode con:cols=100 lines=50
                                @color E
                                @echo off
                                REM   This is the main menu of the program.
                                goto menu
                                :menu
                                echo Brett's Remote Connection tool v1.0
                                echo.
                                echo.
                                echo This program will remotely connect to the PC of your choice.
                                echo.
                                echo  ______________________________
                                echo !                              !
                                echo !  1. Connect to laptop       !
                                echo !  2. Connect to desktop       !
                                echo !  3. Freeform entry           !
                                echo !  4. Exit             !
                                echo !                    !
                                echo !______________________________!
                                echo.

                                REM   This establishes what part of the file each user input will correspond to.
                                :choice
                                set /P C=Enter Selection #:
                                if "%C%"=="4" goto quit
                                if "%C%"=="3" goto freeform
                                if "%C%"=="2" goto desktop
                                if "%C%"=="1" goto laptop
                                goto choice



                                :desktop
                                set /p D=Type the desktop number:
                                start mstsc /v:MAX42MGWK%D% /w:640 /h:480
                                goto menu

                                :laptop
                                set /p L=Type the laptop number:
                                start mstsc /v:MAX42MGLP%L% /w:640 /h:480
                                goto menu

                                :freeform
                                set /p F=Type the entire PC name:
                                start mstsc /v:%F% /w:640 /h:480
                                goto menu

                                :quit
                                exit
                                :end