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 33138 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: 165
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 7
      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: 119
        • Experience: Familiar
        • OS: Windows 7
        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 »
        It has been my experience that folks who have no vices have very few virtues

        - Abraham Lincoln

        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: 119
                • Experience: Familiar
                • OS: Windows 7
                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)

                It has been my experience that folks who have no vices have very few virtues

                - Abraham Lincoln

                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: 119
                  • Experience: Familiar
                  • OS: Windows 7
                  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)
                  It has been my experience that folks who have no vices have very few virtues

                  - Abraham Lincoln

                  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: 119
                    • Experience: Familiar
                    • OS: Windows 7
                    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)
                    It has been my experience that folks who have no vices have very few virtues

                    - Abraham Lincoln

                    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?