一个相对实用的PowerShell时间记录脚本
1 #initialization
2 $timeInterval = 30 #监测间隔
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\timeRecord$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 阅读器 - 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["Documents"]++;}
23 else {if ($_ -match "Microsoft Office OneNote") {$record["Documents"]++;}
24 else {if ($_ -match "Microsoft PowerPoint") {$record["Documents"]++;}
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 #为了防止数据丢失,每10次记录写入文件一次
31 if ($count -eq 0) {$record > "d:\temp\timeRecord$date.txt"}
32 }