Wednesday, February 11, 2009

count events

function CountEvents{


BEGIN {

$errors = 0

$warnings = 0

$info = 0

}

PROCESS {

switch -wildcard ($_.entrytype) {

"err*" {$errors++}

"warn*" {$warnings++}

"info*" {$info++}

default {}

}#switch block

}#process block

END {

"The System log contains " + $errors + " error messages."

"The System log contains " + $warnings + " warning messages."

"The System log contains " + $info + " information messages."

}#end block

}#function block

#get-eventlog "System" -newest 100 CountEvents


======================= anothe approach ==============


$events = get-eventlog "System" -newest 100

function CountEvents{

$count = 0

foreach ($event in $args[0]) {

if ($event.entrytype -like $args[1]) {$count++}

}#foreach block

return $count

}#function block

$errors = CountEvents $events "err*"

$warnings = CountEvents $events "warn*"

$info = CountEvents $events "info*"

"The System log contains " + $errors + " error messages."

"The System log contains " + $warnings + " warning .messages."

"The System log contains " + $info + " information messages."

No comments: