Jump to content

AutoIt error connecting to remote desktop


Go to solution Solved by llssff,

Recommended Posts

Having some issues figuring out automation for RDP on win10. Would appreciate any tips you guys got.

What I am trying to accomplish:

Use an autoIt script to:

  1. open a remote desktop connection
  2. Check 'Don't ask me again for connections to this computer', click connect
  3. Allow for remote desktop to load
  4. Disconnect

Attempted to run the remote desktop connection with mstsc.exe, but got the error

Quote

Invalid connection file(Consoles/administrator@windows-1) specified

 

The shortcut name is 

image.png.49080350cbb44767fea27ea67c795b88.png

The properties

image.png.7e5ee3fc51435c315d38cca4e37ea96e.png

 

My code so far

image.png.e6661e93bf4c242ab9ed81375da0d9d0.png

 

Anyone know why I am getting this error? Or of a better way to perform this task?

image.png

Link to post
Share on other sites

You need to wrap the path to the connection file in quotations and include the file extension. Like so:

Run('MSTSC.exe "C:\Users\Public\Desktop\RDP Consoles\administrator@windows-1.rdp"')

You can then use WinWait to wait for the warning window to appear. Check the warning checkbox using _GUICtrlButton_SetCheck and click the button with ControlClick like you've been trying.

The title of the Remote Desktop Connection once connected should be "administrator - windows-1 - Remote Desktop Connection"? You could use WinWait again to wait for the connected Remote Desktop Connection window to appear and then disconnect using WinClose.

Link to post
Share on other sites

A quickly thrown together example:

#include <GuiButton.au3>

Global $g_hWnd1, $g_hWnd2
Global $g_hCheckBox

Run('MSTSC.exe "C:\Users\Public\Desktop\RDP Consoles\administrator@windows-1.rdp"')

; There's two warning windows that appear

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the first warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the first warning window handle was obtained
    Sleep(2500) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:1]') ; Get the control handle for the checkbox
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:11]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the second warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the second warning window handle was obtained
    Sleep(2500) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:3]') ; Get the control handle for the checkbox
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:5]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('administrator - windows-1', '', 5) ; Wait for the Remote Desktop Connection to appear
If IsHWnd($g_hWnd1) = 1 Then ; Check if the Remote Desktop Connection window handle was obtained
    WinClose($g_hWnd1) ; Close the window
    $g_hWnd2 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the close confirmation window
    If IsHWnd($g_hWnd2) = 1 Then ; Check that we found the above window
        ControlClick($g_hWnd2, '', '[CLASS:Button; INSTANCE:1]') ; Click Yes
    EndIf
    WinWaitClose($g_hWnd1, '', 5) ; Wait for the RDP to close
EndIf

 

Link to post
Share on other sites

@Luke94

For some reason the script stopped working for the rdp window. Using your code above, I managed to find the windows handle. However, for some reason the buttons are not being pressed despite the instance numbers being correct(1 for checkbox, 11 for connect). Do you have any ideas on why it may be bugging out?

Link to post
Share on other sites

It might help ensuring the window is active and focusing the controls before controlling them.

Give this a try. ☺️

#include <GuiButton.au3>

Global $g_hWnd1, $g_hWnd2
Global $g_hCheckBox

Run('MSTSC.exe "C:\Users\Public\Desktop\RDP Consoles\administrator@windows-1.rdp"')

; There's two warning windows that appear

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the first warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the first warning window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    Sleep(2500) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:1]') ; Get the control handle for the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:1]')
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:11]')
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:11]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the second warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the second warning window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    Sleep(2500) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:3]') ; Get the control handle for the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:3]')
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:5]')
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:5]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('administrator - windows-1', '', 5) ; Wait for the Remote Desktop Connection to appear
If IsHWnd($g_hWnd1) = 1 Then ; Check if the Remote Desktop Connection window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    WinClose($g_hWnd1) ; Close the window
    $g_hWnd2 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the close confirmation window
    If IsHWnd($g_hWnd2) = 1 Then ; Check that we found the above window
        ControlFocus($g_hWnd2, '', '[CLASS:Button; INSTANCE:1]')
        ControlClick($g_hWnd2, '', '[CLASS:Button; INSTANCE:1]') ; Click Yes
    EndIf
    WinWaitClose($g_hWnd1, '', 5) ; Wait for the RDP to close
EndIf

 

Link to post
Share on other sites
Posted (edited)

Wow, that was super quick. Thanks. @Luke94

I did some testing with msgboxes. I looks like it indeed was not in focus. It came into focus after the second if, but the buttons still are not being clicked.

The weird thing is, even sending manual commands via ```Send()``` does not seem to work. I tried using tab, space, enter to check the box, then connect., but none of it affected the rdp window.

Wondering what you think of this.

Edited by llssff
Link to post
Share on other sites

I think I had that issue when I was writing it originally - that's why I added the sleeps. It's as if the controls don't load right away.

Try this - I've increased the sleeps from 2.5 seconds to 5 seconds. I've also set the workingdir of the Run command.

#include <GuiButton.au3>

Global $g_hWnd1, $g_hWnd2
Global $g_hCheckBox

Run('MSTSC.exe "C:\Users\Public\Desktop\RDP Consoles\administrator@windows-1.rdp"', 'C:\Users\Public\Desktop\RDP Consoles')

; There's two warning windows that appear

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the first warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the first warning window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    Sleep(5000) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:1]') ; Get the control handle for the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:1]')
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:11]')
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:11]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the second warning window
If IsHWnd($g_hWnd1) = 1 Then ; Check if the second warning window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    Sleep(5000) ; Wait 2.5 seconds - without a wait, the buttons aren't clicked
    $g_hCheckBox = ControlGetHandle($g_hWnd1, '', '[CLASS:Button; INSTANCE:3]') ; Get the control handle for the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:3]')
    _GUICtrlButton_SetCheck($g_hCheckBox, True) ; Check the checkbox
    ControlFocus($g_hWnd1, '', '[CLASS:Button; INSTANCE:5]')
    ControlClick($g_hWnd1, '', '[CLASS:Button; INSTANCE:5]') ; Click the Yes button
EndIf

Sleep(1000)

$g_hWnd1 = WinWait('administrator - windows-1', '', 5) ; Wait for the Remote Desktop Connection to appear
If IsHWnd($g_hWnd1) = 1 Then ; Check if the Remote Desktop Connection window handle was obtained
    If WinActive($g_hWnd1) = 0 Then
        WinActivate($g_hWnd1)
        WinWaitActive($g_hWnd1, '', 3)
    EndIf
    WinClose($g_hWnd1) ; Close the window
    $g_hWnd2 = WinWait('Remote Desktop Connection', '', 5) ; Wait for the close confirmation window
    If IsHWnd($g_hWnd2) = 1 Then ; Check that we found the above window
        ControlFocus($g_hWnd2, '', '[CLASS:Button; INSTANCE:1]')
        ControlClick($g_hWnd2, '', '[CLASS:Button; INSTANCE:1]') ; Click Yes
    EndIf
    WinWaitClose($g_hWnd1, '', 5) ; Wait for the RDP to close
EndIf

 

Link to post
Share on other sites

image.png.f3ac3360fee197de6f06299157a799c4.png

I've tried the changes, but the window is still hovering with no changes.

Are you getting two warning windows on your end?

The workflow I'm trying to automate is:  rdp shortcut -> remote desktop connection(above) -> windows security -> vm open

I have the windows security part done, this window for some reason just doesn't seem to work.

Link to post
Share on other sites
  • Solution

@Luke94

Thanks for the help. I figured it out after some poking around. The issue turned out to be:

  1. Adding msgbox to debug was causing windows to go out of focus. Seems obvious now, but was why I couldn't get your code to work immediately.
  2. Handles behaving weird for rdp window when using [button, instance] combo. Turns out ID is the best way to locate a button according to the doc.

Turns out, Send( ) does wonders.

Here is the snip of the final working code. Thanks again, hopefully we both learned something today.

image.thumb.png.eacdc255fefa18026f70b02c3517d3d2.png

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...