简体   繁体   中英

Handle a COM event in PowerShell

I am controlling an application via its COM server from PowerShell. This is pretty simple:

# Start the application via COM
$app = New-Object -ComObject CoolApp.Application

# Start a data measurement in the application
$measurement = $app.Measurement
$measurement.Start()

Now comes the hard part: the Measurement object has an event called "OnFinished", that is invoked when the data measurement is done. I would like my powershell script to wait for the occurence of this event.

How can I subscribe to such an COM event PowerShell?

If a Primary Interop Assemblies(PIA) is provided from the developer to the COM object you are using, you can easily register event handlers by using it.

For example, refer to this description.
Registering and Catching Communicator 2007 Automation API Events

Like the above components and Office, PIA is provided for many of Microsoft's products.
Office primary interop assemblies

If there is no PIA, you can request it from the developer or you can create it yourself.
The way to make it is explained on this page.
How to: Generate Primary Interop Assemblies Using Tlbimp.exe

Is OnFinished a property of the app or measure object? If it is, and it returns a value when queried you could use a while loop to wait for that returned value to be a specific value eg if that property returns 'Completed' once the process has finished:

while($measure.OnFinished -ne 'Completed')
{
    Sleep 5
}

Difficult to be more specific with my answer as I am not familiar with your COM object :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM