Monitor Event Server via Powershell and notify on failures

Someone posted an interesting question over on the forum: how to monitor event processing without getting directly onto the server.  The OP was searching for a way to do it via C#, but even that is unnecessary.  It's possible to do this directly from powershell.  Even better, you can schedule your powershell script and have it do something when there's a problem. 

Here's a cut-down version of a powershell script I provide to my customers.  In the example below I have it writing to the event log when there are failures and to the information log with the current counters.

$dbid = "CM"
$wrksrv = "WG1"
 
#determine where in the event logs this information will go, create if doesn't exist
$eventLogDisplayName = "HPE Content Manager Event Processor"
$eventLogSourceName = "HPE Content Manager Event Processor"
$logFileExists = Get-EventLog -ComputerName $wrksrv -list | Where-Object {$_.logdisplayname -eq $eventLogDisplayName} 
if (! $logFileExists) {
    New-EventLog -ComputerName $wrksrv -LogName $eventLogDisplayName -Source $eventLogSourceName
}
 
#import CM namespace, create threaded database connection 
Add-Type -Path "C:\Program Files\Hewlett Packard Enterprise\Content Manager\HP.HPTRIM.SDK.dll"
[HP.HPTRIM.SDK.Database]::AllowAccessFromMultipleThreads = $true
$db = New-Object HP.HPTRIM.SDK.Database
$db.Id = $dbid
$db.WorkgroupServerName = $wrksrv
$db.Connect
 
#create an event monitor with refresh interval -- not should not really be any less than the interval defined in the event processing properties
$monitor = New-Object HP.HPTRIM.SDK.EventMonitor -ArgumentList $db, 500
#store list of what is processing and then tell it we want stats
$processors = $monitor.GetAvailableProcessors()
foreach ( $p in $processors ) {
    $monitor.EnableCounters($p)
}
#start gathering stats
$guid = $monitor.Initialise()
#give the event processor some time to process
Start-Sleep -Seconds 1
if ( $monitor.IsAlive() ) {
    foreach ( $p in $processors ) {
        #look for failure results
        $counters = $monitor.GetResults($p)
        $failed = $counters.FailedEvents()
        $queued = $counters.QueuedEvents()
        $processed = $counters.ProcessedEvents()
        if ( $failed -gt 0 ) {
            Write-EventLog -ComputerName $wrksrv -LogName $eventLogDisplayName -Source $eventLogSourceName -EntryType Error -EventId 1 -Message "Detected $($failed) Failed Events"
        }
        if ( $processed -gt 0 ) {
            Write-EventLog -ComputerName $wrksrv -LogName $eventLogDisplayName -Source $eventLogSourceName -EntryType Information -EventId 1 -Message "Detected $($processed) Processed Events"
        }
 
    }
}
               

As a CM administrator, I can install Server Manager on my local Windows 10 workstation.  When I'm in the Server Manager I can use standard windows servers features (instead of custom or third party products) to manage the environment.

The highlighted entry is the one created by the powershell script

The highlighted entry is the one created by the powershell script