0

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?

2
  • I'm finding it hard to figure out what you're trying to accomplish here. What do you mean $eventData.message should alternate values but $eventData[0].message is always down? Commented Feb 6, 2015 at 18:00
  • The array $eventData has the elements "message" that indicates if it's a down event of up event, and a TimeGenerated element that has a timestamp. My goal is to pair these TimeGenerated values so that each iteration of $eventPairs has a down time first, and an uptime second (I calulate the timespan of these two later on) later on. If the very first iteration is an up timestamp, then it will throw off the pairing (every pair will then will have an up time first then a down time, which is backwards). Commented Feb 6, 2015 at 18:07

1 Answer 1

1

I think that might simplify to something like this:

$eventPairs = 
$eventdata |
  foreach {
  if ($_.message -eq "down")
   { $eventPair = @(Get-Date($_.timegenerated) -format u) }

  ElseIf (($_.message -eq "up") -and ($eventPair.count -eq 1))
  { 
    $eventPair += @(Get-Date($_.timegenerated) -format u)
    $eventPair
  }
}

Each "down" event will start a new event pair, which will be completed by the next "up" event. Extra "up" events will be ignored because the pair will already be completed, so it's count will be greater than 1. Early "up" events will be ignored because there hasn't been a down event to start an event pair yet.

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

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.