llssff Posted July 1, 2021 Share Posted July 1, 2021 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: open a remote desktop connection Check 'Don't ask me again for connections to this computer', click connect Allow for remote desktop to load 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 The properties My code so far Anyone know why I am getting this error? Or of a better way to perform this task? Link to comment Share on other sites More sharing options...
Luke94 Posted July 1, 2021 Share Posted July 1, 2021 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 comment Share on other sites More sharing options...
Luke94 Posted July 1, 2021 Share Posted July 1, 2021 A quickly thrown together example: expandcollapse popup#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 llssff and robertocm 2 Link to comment Share on other sites More sharing options...
llssff Posted July 2, 2021 Author Share Posted July 2, 2021 Thank you for the code snippet. It worked like a charm! Link to comment Share on other sites More sharing options...
llssff Posted July 6, 2021 Author Share Posted July 6, 2021 @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 comment Share on other sites More sharing options...
Luke94 Posted July 6, 2021 Share Posted July 6, 2021 It might help ensuring the window is active and focusing the controls before controlling them. Give this a try. ☺️ expandcollapse popup#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 comment Share on other sites More sharing options...
llssff Posted July 6, 2021 Author Share Posted July 6, 2021 (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 July 6, 2021 by llssff Link to comment Share on other sites More sharing options...
Luke94 Posted July 6, 2021 Share Posted July 6, 2021 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. expandcollapse popup#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 comment Share on other sites More sharing options...
llssff Posted July 6, 2021 Author Share Posted July 6, 2021 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 comment Share on other sites More sharing options...
Solution llssff Posted July 7, 2021 Author Solution Share Posted July 7, 2021 @Luke94 Thanks for the help. I figured it out after some poking around. The issue turned out to be: 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. 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. Luke94 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now