Print Counter

R*y一定想要一个命令行的工具来计算每个用户打印了多少张纸 (通过EventLog), 告诉他要买第三方软件就是不肯. 考虑到OOB的问题, 就不在现有系统上帮他实现, 所以在MSH上看了一下, 结果发现真是xxx的方便啊.

一条命令搞定:

1
(get-eventlog systemwhere {($_.source -eq "Print") -and ($_.eventid -eq "10")})foreach {$_.TimeWritten.tostring() +","+ $_.username.tostring() +","+ $_.ReplacementStrings[6].tostring()}sort-object -property TimeWritten>PrintCount.csv

用Excel打开, 做图表啊做图表.

一劳永逸 (记得放到profile.msh里啊):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function print-count
{
$local:summary = @{}
$local:sum = 0
if ( $Args.length -eq 2)
{
$local:elog=(get-eventlog systemwhere {($_.source -eq "Print") -and ($_.eventid -eq "10") -and ($_.timegenerated -ge $args[0]) -and ($_.timegenerated -le $args[1])})
}
else
{
$local:elog=(get-eventlog systemwhere {($_.source -eq "Print") -and ($_.eventid -eq "10")})
}
if ($local:elog.count -gt 0)
{
foreach ($local:log in $local:elog)
{
if(-not $summary.containskey($local:log.username.tostring()))
{
$summary.add($local:log.username.tostring(),0)
}
$summary[$local:log.username.tostring()]+=$local:log.ReplacementStrings[6]
$sum+=$local:log.ReplacementStrings[6]
}
}
$local:format = @{Expression={$_.Key}; Label = "User Name"; Width = 20}, @{Expression={$_.Value}; Label = "Printed Pages"; Width = 25}
$local:summarysort-object -property Valueformat-table $local:format
write-host "----------------------------------------------"
write-host ("Total Pages"+" "*(35-$local:sum.tostring().length)+$local:sum.tostring())

然后就可以:
print-count
或者
print-count ("2005-1-1","2005-12-31")