title: Selecting okay button automatically in a pop-up box with powershell
body: <p>I am creating a PowerShell script to access a Sybase IQ Server. Normally with an average database I would be doing this through isql which eliminates the UI. But since this is a data warehouse it appears that I have a UI to fight with.</p>
  
  <pre><code>dbisql -c "uid=$usrID;pwd=$pwd;eng=iq_datawarehouse; links=tcpip(host=$ip;port$port)"
  </code></pre>
  
  <p>The above code brings up a UI but since this is connecting to a server through a local machine comes the issue with a confirmation box. This script needs to be completely automated so the UI that appears needs to auto select "Okay" to proceed. </p>
  
  <p>Taking from Nate Hekman's registering, finding, and sending commands to a window I have been able to get the script to work most of the time.</p>
  
  <pre><code>    $pinvokes = @'
          [DllImport("user32.dll", CharSet=CharSet.Auto)]
          public static extern IntPtr Connect(string className, string Connect);
  
          [DllImport("user32.dll")]
          [return: MarshalAs(UnmanagedType.Bool)]
          public static extern bool SetForegroundWindow(IntPtr hWnd);
  '@
      Add-Type -AssemblyName System.Windows.Forms
      Add-Type -MemberDefinition $pinvokes -Name NativeMethods -Namespace MyUtils
      $hwnd = [MyUtils.NativeMethods]::FindWindow("Connect")
      [MyUtils.NativeMethods]::SetForegroundWindow($hwnd)
      [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
  </code></pre>
  
  <p>The issue here is when I run this the SendWait("{Enter}") executes prematurely resulting in creating a line break in PowerShell ISE 60% of the time.</p>
  
  <p>My question here is how would you go about streamlining this to make it more effective and not a hit and miss gamble? Is there a way to select okay rather than sending "Enter" after the window has been identified and brought to the foreground? </p>
  
  <p>Thank you for your help! </p>