Jump to content

Automate Remote Desktop Connection


Recommended Posts

Hello,

I am trying to use Autoit to create an exe file that I can pass a hostname username and password and it will automatically remote (Terminal Services) into a PC/server. So far I have the following code that works but I am having the following problems:

  • If the server is unreachable the script still sits and waits for the an active window e.g. Autoit icon remains in taskbar until manually closed
  • If the connection is slow the script fires the username and password too early. I have tried to increase the sleep period but is there any way I can get it to wait until the 'username' and 'password' prompt is displayed before continuing?
Script so far:

Run("mstsc.exe /console")

$server = $CmdLine[1]

$username = $CmdLine[2]

$password = $CmdLine[3]

Send($server)

send("{enter}")

WinWaitActive($server & " - Remote Desktop")

sleep(500)

Send("!u")

sleep(200)

send($username)

send("{tab}")

Sleep(500)

send($password)

send("{enter}")

Many thanks

Link to comment
Share on other sites

This will help with the first problem as using timeouts with waiting for windows to come along will let the script continue if failure happens.

If $CMDLINE[0] = 3 Then
    $server = $CMDLINE[1]
    $username = $CMDLINE[2]
    $password = $CMDLINE[3]
    ;
    Run("mstsc.exe /console")
    If WinWait("Remote Destop Connection", "", 5) Then
        WinActivate("Remote Destop Connection")
        If WinWaitActive("Remote Destop Connection", "", 5) Then
            Send($server)
            Send("{enter}")
            ;
            If WinWaitActive($server & " - Remote Desktop", "", 60) Then
                Sleep(500)
                Send("!u")
                Sleep(200)
                ;
                Send($username)
                Send("{tab}")
                Sleep(500)
                ;
                Send($password)
                Send("{enter}")
            EndIf
        EndIf
    EndIf
EndIf

For problem two, which I have no available Remote Desktop connection to use to help you with, is to possible use ControlCommand() to see if the controls are enabled (IsEnabled). Use a While...WEnd loop with ControlCommand() to continuely check the control until enabled, and then send your name and password.

If you wanted this script to be better, then look at only using WinWait() and the Control*() functions as they do not require active windows so your script success will increase by 10 fold.

:whistle:

Link to comment
Share on other sites

In MSTSC window is only one control (like in IE browser), so you can't use Control...() commands.

Thanks for all the help. I am still seeing problems with the connection. It can take between 1 and 15 seconds for the username and password prompt to fully display. Is there any way I can determine when these boxes become active?

Many thanks

Link to comment
Share on other sites

Is there any way I can determine when these boxes become active?

Many thanks

I just tried seeing what AutoIT Info could find in the dialog box, and it really can't distinguish between an empty box and the log in box. Therefore, I would suggest doing a pixel search for one of the colors in the Windows login icon (Red, yellow, green or blue) and launch your code after you see that color appear...
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

  • 2 months later...

You could try some of the stuff i use in my script

$servername = "servername"

$username = "username"

$password = "password"

run("mstsc /console /v:"& $servername)

WinWaitClose("Connecting")

Opt("WinTitleMatchMode", 4)

WinWaitActive("classname=TSSHELLWND")

Sleep(2000)

Send("!u")

Send($username)

Sleep(500)

Send("!p")

Send($password)

Send("{ENTER}")

Opt("WinTitleMatchMode", Default)

It works well enough although it might need some tweaking

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...