I need to implement an event handler for this NewCivicAddressReport event of the CivicFactory object. This following code, using event handler naming convention, works perfectly in VBScript:
Dim CivicFactory
Set CivicFactory = WScript.CreateObject("LocationDisp.CivicAddressReportFactory", "CivicFactory_")
Function CivicFactory_NewCivicAddressReport(report)
MsgBox "Location changed!"
End Function
However in PowerShell the following code fails:
$CivicFactory = new-object -comObject LocationDisp.CivicAddressReportFactory
Register-ObjectEvent $CivicFactory -EventName "NewCivicAddressReport" -Action ({ echo "hello" })
The error message is : Register-ObjectEvent : Cannot register for event. An event with name 'NewCivicAddressReport' does not exist.
I also tried $CivicFactory.add_NewCivicAddressRerport( {"hello"} ) and it failed too.
So I turned to $CivicFactory | Get-Member: it did return its methods and properties but NO events.
So I suspect PowerShell doesn't support COM events very well. I installed the pseventing snapin and tried Get-EventBinding CivicFactory -IncludeUnboundEvents | Format-Table -Auto - it returned nothing, which means the system doesn't believe this object has events.
So now I'm doubting: is it possible at all to bind an event handler to an object?
Can anyone show me the correct way?