Since I have to keep track of when I started working and when I stopped working (because of some legal regulations), I was using an app on my iphone to check in and out of work. But often I either was in a hurry and forgot it or forgot my phone at home or had no battery left… So I’ve decided to try and automate this.
I’d need to cover the following:
- Log an entry on the first login on the machine every day.
- Log an entry on the last logout on the machine every day.
- account for the fact that I usually do not logout but send the computer into sleep mode
- and in the morning I then wake from sleep
First we’ll create two batch files logging the times we’ve started and ended work.
Create a login.bat file somewhere with the following contents:
type %USERPROFILE%\loginlogoff.log | findstr /R "login.*%date%" if errorlevel 1 echo login at %time% %date% >> %USERPROFILE%\loginlogoff.log
This basically checks whether the file %USERPROFILE%\loginlogoff.log already contains an entry with login and today’s date. If yes, there is nothing to do since it’s not the first login event today. If not, we write an entry in the file with the current time and date.
Then create a logout.bat file with the following contents:
type %USERPROFILE%\loginlogoff.log | findstr /v /R "logout.*%date%" > %USERPROFILE%\loginlogoff.tmp type %USERPROFILE%\loginlogoff.tmp > %USERPROFILE%\loginlogoff.log echo logout at %time% %date% >> %USERPROFILE%\loginlogoff.log
It first creates a copy of the file excluding all logout events which occured today, copies the file and adds a logout event. This basically will replace an logout event which occured on the same day with the latest one.
Now we need to have these scripts executed automatically. My computer at work is a Windows machine, so the obvious choice was to go some scheduled tasks. You can create a scheduled task on logon. But there’s no option for logoff. One option for the logoff issue could be to use the ONEVENT option of schtasks.exe. But it’s kind of a pain (never really worked reliably).
To start it, click on the Start button, select Run… and type gpedit.msc (then press OK). You then have to navigate to Local Computer policy / User Configuration / Windows Settings / Scripts (Logon/Logoff).
There you will see two entries:
Right click on Logon, select Properties, Then click Add, Browse and select the login.bat file created above. Press OK twice.
The do the same thing for Logoff and select the logout.bat file created above.
This takes care of the scenarios 1 and 2 above. Now we need to handle the sleep and wake triggers. There will be handled by scheduled tasks created using schtasks.exe:
SCHTASKS /Create /TN LogWake /TR %USERPROFILE%\login.bat /SC ONEVENT /EC System /MO *[System/EventID=1] SCHTASKS /Create /TN LogSleep /TR %USERPROFILE%\logoff.bat /SC ONEVENT /EC System /MO *[System/EventID=42]
Here you’ll have to replace %USERPROFILE%\login.bat and %USERPROFILE%\logout.bat by the paths to the two batch files created above.
The event ID 1 will be triggered a few seconds after a wake from sleep. It might be trigger many times during the day but we do not care since we’ll only log the first one.
The event ID 42 will be triggered when the computer goes into sleep mode.
That’s it. You’ll then get a file at %USERPROFILE%\loginlogoff.log containing something like this:
login at 8:31:01,33 08.11.2012 logout at 18:37:41,49 08.11.2012