I have the array $eventData in a Powershell 3 script, with elements TimeGenerated and message. $eventData.message should firstly alternate between the values down and up, and secondly $eventData[0].message should always equal down. If this order is off by two consecutive 0s or 1s, or if $eventData[0].message=up, I'll get incorrect results when calculating the timespan (done after these results are sent to MySQL). If either of these two criteria are not met, I'd like to ignore that $eventData.message value and advance to the next iteration until both conditions are met.
$eventPairs = @()
for ($e=0; $e -le $eventData.length; $e++)
{
if ($eventData[$e].message -eq "down")
{
$down2 = (Get-Date($eventData[$e].timegenerated) -format u)
}
ElseIf ($eventData[$e].message -eq "up")
{
$up2 = (Get-Date($eventData[$e].timegenerated) -Format u)
}
if($down2 -AND -$up2)
{
#we have both down and up values, lets pair them
$eventPairs +=,@($down2,$up2)
$down2 = $up2 = $null
}
}
I've thought about doing a if eventData[e$]=0 -AND eventData.message -eq "up" then continue, but the first two or three iterations could be up values, which also throws off my idea for the second criteria, which would be an even/odd test ( since $e=[1]=odd then eventData.message should equal up, else continue).
I'm stumped. How can I best achieve this?