Jump to content

Capture Your Own Console Output


seadoggie01
 Share

Recommended Posts

I'm working on a project right now for another department at work and I'm not in IT so I don't have great remote access to their computers. I'm trying to get a finicky internal website to be automated with some financial data, but Finance needs to be running the script since they have access to the non-testing site. Currently, the amazing WebDriver UDF uses ConsoleWrite to output debugging data, which works great on my computer. Finance isn't tech savy and would really like a one-file solution. I need to be able to capture this output and email it back to myself in the case of an error (which happens a lot).

This code can be inserted to the beginning of my script to allow me to run my script from the exe, capture the output, and (with another function) email the results back to myself. In case anyone is interested, it's kind of a neat way to do things, I thought :)

; Required for using /AutoIt3ExecuteScript on another AutoIt3 Script
#pragma compile(AutoItExecuteAllowed, True)
; Run Au3Stripper
#AutoIt3Wrapper_Run_Au3Stripper=y
; Merge only, just add in all required constants and functions
#Au3Stripper_Parameters=/mo

#include <AutoItConstants.au3>

ParentOnlyCode()

Main()

Func ParentOnlyCode()

    ; The parent is compiled. Skip this if not compiled.
    If Not @Compiled Then Return

    ; The child is named exactly like the source code (ScriptName.au3)
    Local $sAu3File = StringReplace(@ScriptFullPath, ".exe", ".au3")

    ; File Install the source code to the current directory
    FileInstall(@ScriptFullPath, $sAu3File)

    ; The command to execute
    Local $sCmd = '"' & @AutoItExe & '" /AutoIt3ExecuteScript "' & $sAu3File & '"'

    ; Run the command and capture the Console output
    Local $iPID = Run($sCmd, "", @SW_HIDE, $STDERR_MERGED)
    If @error Then
        ; Do something here in case of a Run error
        Exit MsgBox(0, "", "Run Error: " & @error)
    EndIf

    ; Wait for the script to close
    ProcessWaitClose($iPID)
    ; Capture the exit code
    Local $iExitCode = @extended

    ; Read the Console output
    Local $sText = StdoutRead($iPID)

    $sText &= @CRLF & @CRLF & "Exit code: " & $iExitCode

    ; Write it to a file
    FileWrite(@ScriptDir & "\output.txt", $sText)
    
    ; Optionally, delete the Au3 file here
    FileDelete($sAu3File)
    
    ; Done. Don't execute the Main script.
    Exit

EndFunc

Func Main()

    ; This text would normally be lost in a Compiled script, it will be written to output.txt
    ConsoleWrite("Doing Main stuff" & @CRLF)

EndFunc

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...