Fedric Kelley
Fedric Kelley

Reputation: 1

Use powershell to scroll to bottom of Toad output from the command line

I'm trying use the following commands to start Toad and then scroll down to the bottom of the output. The command starts Toad and executes the query but does not scroll down to the bottom of the output.

$argList2 = "-NoExit -Command {Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.SendKeys]::SendWait('^{END}') }"

Start-Process Toad -ArgumentList "-min","-c","admin/xxxx@yyyyy","-f","C:\Users\Documents\test.sql","/EXEC", $argList2

Upvotes: 0

Views: 32

Answers (1)

mklement0
mklement0

Reputation: 439812

  • I'm assuming that Toad is a GUI application (because if it were a console application, scrolling would happen automatically).

  • As such, you don't need Start-Process to launch it asynchronously; even directly invoked GUI applications launch asynchronously by default.

  • The keystrokes must be sent from the caller, not from the child process that launches Toad.

    • Aside from that, your approach fundamentally cannot work: you're telling Start-Process to launch Toad, which precludes passing additional commands as part of the call. To make Start-Process launch multiple commands, it must launch a shell executable to which a command line comprising multiple commands can be passed. To pass a PowerShell command line (as you've attempted), you must target the PowerShell CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7)
  • Sending keystrokes to application is inherently brittle, and should only be used as a last resort.

    • In the case at hand, there is no easy way to determine when the query has completed executing; the best you can do is sleep (wait) a bit, in the hopes that the application is in the desired state when the keystrokes are sent.

    • A robust solution would require a more elaborate approach based on the Win32 UI Automation APIs.

      • A managed (.NET) API wrapping the Win32 API exists, but is seemingly limited to v2 of the latter.

      • Third-party .NET libraries, such as FlaUI, provide access to v3 too; see this answer for an example.

Therefore, if you want to stick with sending keystrokes, something like the following may work:

# Asynchronously launch Toad (which is assumed to be a GUI app).
Toad -min -c admin/xxxx@yyyyy -f C:\Users\Documents\test.sql /EXEC

# Sleep a little to give the application time to launch
# and execute the query.
# NOTE: Tweak this as needed.
Start-Sleep -Seconds 3

# Send the scrolling key chord to the newly launched Toad window.
(New-Object -ComObject WScript.Shell).SendKeys('+{END}')

Upvotes: 0

Related Questions