How to fire an event from commandlet and handle it in an application invoking command through pipeline

Jul 8, 2009 at 10:30 AM

I have hosted powershell in a .Net application.

I want to fire an event from a commandlet and want to trap it in an application that is invoking script through pipeline.

I dont know how to handle this.

Please help me out to find a solution.

Thanks in advance

Jul 21, 2009 at 1:38 PM
Edited Jul 21, 2009 at 1:48 PM


This shouldn't be difficult to do. PowerShell script can access any of the classes in the hosting application. I suggest you create a singleton type and use powershell script to raise an event on it. Before you run the script, you would hook an event on it from your host application. Follow me?


public class Notifier {
  public static readonly Notifier Instance = new Notifier();
  private Notifier() { }
  public event EventHandler MyEvent = delegate { }
  public void FireEvent() { MyEvent(this, null); } 

and in PowerShell: