Add-EventHandler dictionary issue

Jun 10, 2008 at 11:59 PM
Edited Jun 11, 2008 at 12:00 AM

I'm wondering if there is a way to use PSEventing given my situation. In the code below you'll notice that in the foreach loop I attach and event handler during each iteration but this fails because the code for the Add-Eventhandler is a hashtable and you cannot add the same event name more than once. I'm hoping someone can provide me with more details on this line $events = @(read-event -wait) and what properties are avialable for each event that it returns. Is it possible to re-write Add-Eventhandler to use a unique event guid as the hashtables key rather than an event name?

foreach ($ApplicationEntry in $($Project.Applications).GetEnumerator()))
{
 ...

 $Step = $MiscPackage.Steps.Get_Item("Step XYZ")
 
 Add-EventHandler (get-variable Step) StepFailed { param($var) Write-Error "The step $($var.Value.Name) failed!" }

 $Step.Execute();

}

Write-Host "Waiting for events.."
Do-Events $true

thx, joey

Jun 11, 2008 at 8:34 PM


Ok, I looked at the source and it will be impossible to bind the same event name to the same ps variable more than once without completely re-writing the source code. I did resolve this, just needed to dynamically create my variable in the for loop so that each ps variable is unique.

# dynamic variable index
$StepIndex = 0

foreach ($ApplicationEntry in $($Project.Applications).GetEnumerator()))
{
 ...
 
 $New-Variable "Step$StepIndex" -value $MiscPackage.Steps.Get_Item("Step XYZ")

 # get a reference to the dynamic variable
 $Step = (Get-Variable "Step$StepIndex" -ValueOnly)

 ... do some stuff to the step ...

 Add-EventHandler (Get-Variable "Step$StepIndex") StepFailed { param($var) Write-Error "The step $($var.Value.Name) failed!" }
 
 $Step.Execute();

 # increment our step index by one
 $StepIndex++
}

Write-Host "Waiting for events.."
Do-Events $true

cheers,
joey