You cannot have a colon in the metric path. APM specifically uses that character as the metric name/value separator.
Here is line 6 from your script:
$metric = 'WinEvents|Application Log|Errors|:Event 1022=' + $msgCount
Here is the same line from the example:
$metric = 'WinEvents|Application Log|' + $enum + ':Message=' + $metricValue
The issue lies with your metric name. It appears that you forgot to provide one before the colon.
You can either remove the pipe symbol before the colon or provide a metric name there.
I believe what you want to do is remove the pipe so you can this metric path, name, and value:
$metric = 'WinEvents|Application Log|Errors:Event 1022=' + $msgCount
so:
domain|host|customprocess|agentname|WinEvents|Application Log|Errors:Event 1002 where 'Event 1002' is an IntCounter.