Recording PC idle time


This script is written in AutoIT and checks for when the PC has been idle (no keyboard presses or mouse movements) for a specific length of time. Currently the script logs to screen but you should be able to easily get it to log to a file and database, although the latter will require more work from you to get it working.

Here’s the script:

#AutoIt3Wrapper_Au3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6

#include <Timers.au3>

$data = InputBox("Threshold", "Record user input if the PC has been idle for (in seconds)")

; Mouse/Keyboard action during this 10 sec delay will change reported idle time
Global $SleepDuration = 10 * 1000 ; 10sec
Global $TotalIdleTime = 0
Global $Count = 0
Global $Threshold = $data*1000

; Create your database manually then add these
Global $MyDB="UserIdle"
Global $MyUser="Testuser"
Global $MyPwd="TestPass"
Global $MySvr="MyDBServer"

while 0 = 0
    Sleep($SleepDuration)
    Global $IdleTime = _Timer_GetIdleTime()

    ConsoleWrite("DEBUG: idle = " & $IdleTime & @LF)

    if $IdleTime > $SleepDuration-2 Then
        $TotalIdleTime = $IdleTime
    else 
        if $TotalIdleTime > $Threshold then
            ConsoleWrite ("Machine returned from idle after " & int(($TotalIdleTime + ($SleepDuration - $IdleTime))/1000) & " seconds" & @LF)
        EndIf
        $TotalIdleTime = $IdleTime
    endif
wend

func write_to_db()
    $conn = ObjCreate( "ADODB.Connection" )  
    $DSN = "DRIVER={SQL Server};SERVER=MySvr;DATABASE=MyDB;UID=MyUser;PWD=MyPwd;"  
    $conn.Open($DSN)  
    $rs = ObjCreate( "ADODB.RecordSet" )  
    ;You'll need to change this insert to match the name of the table and the columns in your database
    $rs.Open( "INSERT some data into my database", $conn )  
    $conn.close 
EndFunc

On running the script prompts with a dialogue box for the number of minutes to wait before treating the PC as idle and logging the event.

Compiling

Grab and install AutoIT from their website. Use the AutoIT editor to tweak the script how you’d like. To compile open “AutoIT Script to exe converter” from the AutoIT start menu group. Choose the options as you see fit and compile. There are compilers for x64 and x32 which you’ll find in the <program files>\AutoIt3\Aut2Exe directory.

Heres the source and binaries to download:

Rename from doc to zip

Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: