title: A trouble with automation of Windows RDP through console
body: <p>A brief description of my problem:</p>
  
  <p>1.
  My Jenkins job is required to establish an RDP connection to another machine to perform some activities.</p>
  
  <p>2.
  Until recently, the default password was maintained between sessions. But now some settings have changed, and the password needs to be reentered by hand each time I creating a new RDP session.</p>
  
  <p>I prepared a short python script interacting with the Windows gui via the win32gui package.</p>
  
  <p>I built a stand alone executable file from this script using the pyinstaller.
  And finally I added a call to this executable file directly to the job. </p>
  
  <p>Somethig like that:</p>
  
  <pre><code>while attempts:
      security_window_title = "Windows Security"
      try:
          hwnd_credentials = win32gui.FindWindow(0, security_window_title)
  
          window_controls = []
          win32gui.EnumChildWindows(hwnd_credentials, collect_window_control, None)
  
          focus_on_window(hwnd_credentials)
          sleep(0.5)
          prev_user_login = window_controls[2]["hwnd"]
  
          x = int(window_controls[1]["x"] + 80)
          y = int(window_controls[1]["y"] + 20)
  
          click(x, y)
          type_message(password)
  
          ok_button = window_controls[6]["hwnd"]
          push_button(ok_button)
  
      except win32gui.error:
          sleep(1)
          attempts -= 1
          if not attempts:
              raise RuntimeError("Can't interact with window: {}.".format(security_window_title))
      else:
          break
  
  while attempts:
      sleep(timeout)
      attempts -= 1
      if check_connection_started():
          break
  
      if check_certificate_errors():
          for control in window_controls[::-1]:
              if control["text"] == "&amp;Yes":
                  push_button(control["hwnd"])
  
      if not attempts:
          raise RuntimeError("Connection not established.")
  </code></pre>
  
  <p>3.
  This would not be a problem when script running from the job working with the fully functional Windows ui. I can find a window in which my script is supposed to specify a password using the win32gui python package. I can generate all the appropriate keyboard events to enter a password.</p>
  
  <p>Using RDP via console provides me a very strange set of windows-like objects which I can not interact with using the win32gui python package the same way as with ordinary windows. For example, I do locate a window with non zero hwnd and with text property equal to "Remote Desktop Connection". But I can't focus on such a window using the basic method win32gui.SetForegroundWindow(hwnd). This leads to an unnamed  win32gui exception.</p>
  
  <p>Is there any possibility to transfer the password to the desired control of the desired window-like structure, so that the job does not interrupt its execution?</p>
  
  <p>Thank you so much for any help.</p>