2

I had a COM object created in PowerShell: $CivicFactory = new-object -comObject LocationDisp.CivicAddressReportFactory

I need to implement the event handler for "NewCivicAddressReport". I tried this:

Register-ObjectEvent $CivicFactory -EventName "NewCivicAddressReport" -Action ({ echo "haha" })

But it gave me "Register-ObjectEvent : Cannot register for event. An event with name 'NewCivicAddressReport' does not exist."

Then I tried $CivicFactory.add_NewCivicAddressRerport( {"haha"} )

It did not work either: Method invocation failed because [System.__ComObject#{bf773b93-c64f-4bee-beb2-67c0b8df66e0}] doesn't contain a method named 'add_NewCivicAddressRerport'.

Can anyone tell me how to implement this event handler in PowerShell? Thanks in advance. In VBScript it is as simple as

Function CivicFactory_NewCivicAddressReport(report)
    Blablabla...
End Function

But in PowerShell I couldn't get it working.

3 Answers 3

1

You might be interested in PSEventing, it adresses .NET, COM and WMI object event handling.

Sign up to request clarification or add additional context in comments.

1 Comment

I would try $CivicFactory | Get-Member -Type Event to get all the events to which your object can be subscribed.
1

Use the Interop assembly generated by Visual Studio.

Whenever a COM (library) reference is added to a C# project the IDE builds a wrapper that makes all entities, including events and enumerations, available to the .NET world.

The wrapper is usually packed in a file named like this:

Interop.<COM-library-name>.dll

Note that class names might be different. Use the IL Disassembler (ildasam.exe) to identify the class you have to instantiate.

Comments

0

I tried Get-Member:$CivicFactory | Get-Member -MemberType Event But it returned nothing.

If I run Get-Member without specifying member type, it does return its methods and properties. And the methods and properties work well so I believe the object is OK.

So it looks like the object's events are not recognized by the PowerShell system.

But the document states the event handler NewCivicAddressReport should be implemented, and I have done this successfully in VBScript and JScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.