1 # initialization
2 $ timeInterval = 30 # monitoring interval
3 $ record = @ {"Coding" = 0; "Outlook Email" = 0; "Gmail" = 0; "Google Reader" = 0; "BBS" = 0; "Other Internet" = 0; "Documents" = 0 ;}
4 $ count = 0
5 $ date = date-format "yyyyMMdd"
6 # try to resume
7 if (test-path "d: \ temp \ timerecord=date.txt "){
8 gc "d: \ temp \ timerecord1_date.txt" |%{ if ($ _-match "\ w + \ s + \ d + "){
9 $ groups = [Regex]: Match ($ _, "^ (\ w + \ s? \ W +) \ s + (\ d +) "). Groups;
10 $ record [$ groups [1]. Value] = [int]: Parse ($ groups [2]. Value );
11 }}
12}
13 # start to monitor
14 while ($ true)
15 {
16 $ titles = ps |? {$ _. MainWindowTitle} | select MainWindowTitle
17 $ titles | % {
18 if ($ _-match "Google Reader-Windows Internet Explorer") {$ record ["Google Reader"] ++ ;}
19 else {if ($ _-match "Gmail-Windows Internet Explorer") {$ record ["Gmail"] ++ ;}
20 else {if ($ _-match "Internet Explorer") {$ record ["Other Internet"] ++ ;}
21 else {if ($ _-match "Visual Studio") {$ record ["Coding"] ++ ;}
22 else {if ($ _-match "Microsoft Word") {$ record ["events"] ++ ;}
23 else {if ($ _-match "Microsoft Office OneNote") {$ record ["events"] ++ ;}
24 else {if ($ _-match "Microsoft PowerPoint") {$ record ["events"] ++ ;}
25 else {if ($ _-match "Message (HTML)") {$ record ["Outlook Email"] ++ ;}
26 else {if ($ _-match "bbs") {$ record ["BBS"] ++ ;}
27 }}}}}}}}
28}
29 sleep ($ timeInterval)
30 $ count = ($ count + 1) % 10 # To prevent data loss, write files every 10 times
31 if ($ count-eq 0) {$ record> "d: \ temp \ timerecord1_date.txt "}
32}