Jump to content

AUTOIT Logging


Recommended Posts

I would just have it save it to a text file. nothing big, just after each send, add a line saying $logging = "Your Send Text" so you can see what you sent. then at the end, it saves it all into the text document. IF you want to delete the text document after each use or what ever then use the bottom 2 lines at the bottom.

The code is pretty self explanitory, it saves the text into $logging, then saves that text + (enter).

THen, it saves the new text into $logging, and when it is saving $fileContents, it uses the previous one + the new text.

im not that great at explaning stuff, but if you need any more help or if this didnt answer your question, i could take more time making it clearer.

$TextFileName = "Log.txt"
;Send("Hello")
$Logging = "Hello"
$FileContents = String($Logging &@CRLF)
;Send("Whats up?")
$Logging = "Whats up?"
$FileContents = String($FileContents & $Logging& @CRLF)
;Send("How Are you Doing?")
$Logging = "How Are you Doing?"
$FileContents = String($FileContents & $Logging &@CRLF)
;Send("Im doing good, How are you?")
$Logging = "Im doing good, How are you?"
$FileContents = String($FileContents & $Logging &@CRLF)
;Send("Im great, Ill see you tomorrow, OK?")
$Logging = "im great, Ill see you tomorrow, OK?"
$FileContents = String($FileContents & $Logging& @CRLF)

FileWrite($TextFileName, $FileContents)

;FileDelete($TextFileName)
;FileWrite($TextFileName, "")

~Shakala

Edited by Shakala
Link to comment
Share on other sites

To have it actively log as the program is running, just make a function that logs to a file and also sends the data. Like this:

#include <Date.au3>

SendAndLog("These keys are sent to Log.txt in the script directory")
SendAndLog("These go to log.txt in the script dir also, and include a timestamp.", -1, True)
SendAndLog("These keys are sent to a different log that I specify in the next parameter", @DesktopDir & '\testlog.txt')
SendAndLog("These keys go to the specified directory and have a timestamp", @DesktopDir & '\testlog.txt', True)

Func SendAndLog($Data, $FileName = -1, $TimeStamp = False)
    If $FileName == -1 Then $FileName = @ScriptDir & '\Log.txt'
    Send($Data)
    $hFile = FileOpen($FileName, 1)
    If $hFile <> -1 Then
        If $TimeStamp = True Then $Data = _Now() & ' - ' & $Data
        FileWriteLine($hFile, $Data)
        FileClose($hFile)
    EndIf
EndFunc
Link to comment
Share on other sites

To have it actively log as the program is running, just make a function that logs to a file and also sends the data. Like this:

#include <Date.au3>

SendAndLog("These keys are sent to Log.txt in the script directory")
SendAndLog("These go to log.txt in the script dir also, and include a timestamp.", -1, True)
SendAndLog("These keys are sent to a different log that I specify in the next parameter", @DesktopDir & '\testlog.txt')
SendAndLog("These keys go to the specified directory and have a timestamp", @DesktopDir & '\testlog.txt', True)

Func SendAndLog($Data, $FileName = -1, $TimeStamp = False)
    If $FileName == -1 Then $FileName = @ScriptDir & '\Log.txt'
    Send($Data)
    $hFile = FileOpen($FileName, 1)
    If $hFile <> -1 Then
        If $TimeStamp = True Then $Data = _Now() & ' - ' & $Data
        FileWriteLine($hFile, $Data)
        FileClose($hFile)
    EndIf
EndFunc

This is perfect and works, but it only logs the actual command and not the ouput. So a winrm command is ran (that gets logged) and then after the winrm command is ran, it outputs a code (which doesnt get logged). Any suggestions? Thanks for your help.

Link to comment
Share on other sites

Oh I wasn't aware you were trying to get a return from a command prompt... There is no reason to use Send() for this.

You can use run() to send commands to a command prompt (cmd.exe) and StdoutRead() to grab the returned data. You can also just pipe out the return to a text file through cmd, but for the sake of autoit doing all the logging, I made this example:

#include <Constants.au3>
#include <date.au3>

CMDAndLog('winrm', -1, True)
CMDAndLog('winrm help remoting', -1, True)
CMDAndLog('winrm help switches', -1, True)

Func CMDAndLog($Data, $FileName = -1, $TimeStamp = False)
    If $FileName == -1 Then $FileName = @ScriptDir & '\Log.txt'
    $hCMD = Run(@ComSpec & " /c " & $Data, '', @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Local $Return
    While 1
        $Return &= StdoutRead($hCMD)
        If @error Then ExitLoop
    WEnd
    $Return = StringReplace($Return, @CR, @CRLF & @TAB); Place a tab in each line of the output for a nicer log format
    $hFile = FileOpen($FileName, 1)
    If $hFile <> -1 Then
        If $TimeStamp = True Then $Data = @CRLF & _Now() & ' - ' & $Data & @CRLF & 'Output: ' & $Return
        FileWriteLine($hFile, $Data)
        FileClose($hFile)
    EndIf
EndFunc   ;==>CMDAndLog
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...