Это не означает, что 0-й индекс является «Connect / Reconnect» - если это так, он выдаст исключение из-за отсутствия чего-либо перед ним.
$t = (@"
Time, DateFormat, "Event Type"
3/31/2020 11:59:35 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 4:01:56 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 4:14:19 PM, 31/03/2020, AGENT_CONNECTED
3/31/2020 4:24:12 PM, 31/03/2020, AGENT_CONNECTED
3/31/2020 4:30:31 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 4:36:08 PM, 31/03/2020, AGENT_RECONNECTED
3/31/2020 5:12:49 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 6:25:39 PM, 31/03/2020, AGENT_RECONNECTED
3/31/2020 7:09:08 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 7:38:53 PM, 31/03/2020, AGENT_RECONNECTED
3/31/2020 7:44:44 PM, 31/03/2020, AGENT_CONNECTED
3/31/2020 8:23:48 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 9:33:36 PM, 31/03/2020, AGENT_RECONNECTED
3/31/2020 9:35:58 PM, 31/03/2020, AGENT_DISCONNECTED
3/31/2020 10:46:54 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 10:50:54 PM,31/03/2020, AGENT_DISCONNECTED
3/31/2020 10:53:10 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 11:05:56 PM,31/03/2020, AGENT_DISCONNECTED
3/31/2020 11:13:47 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 11:32:41 PM,31/03/2020, AGENT_DISCONNECTED
3/31/2020 11:34:27 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 11:35:55 PM,31/03/2020, AGENT_DISCONNECTED
3/31/2020 11:38:21 PM,31/03/2020, AGENT_RECONNECTED
3/31/2020 11:50:52 PM,31/03/2020, AGENT_DISCONNECTED
3/31/2020 1:15:02 PM, 31/03/2020, AGENT_CONNECTED
3/31/2020 1:04:17 PM, 31/03/2020, AGENT_DISCONNECTED
"@) | ConvertFrom-Csv
$o = for ($i = $t.Count; $i -gt 0; $i--) {
if ($t[$i].'Event Type' -match 'AGENT_CONNECTED|AGENT_RECONNECTED') {
if ($t[$i-1].'Event Type' -match 'AGENT_DISCONNECTED') {
if ($t[$i-2].'Event Type' -match 'AGENT_CONNECTED|AGENT_RECONNECTED') {
[pscustomobject]@{
'InitiatingIdx' = $i
'InitiatingDtm' = (Get-Date $t[$i].Time)
'DisconnectIdx' = $i-1
'DisconnectDtm' = (Get-Date $t[$i-1].Time)
'ReconnectIdx' = $i-2
'ReconnectDtm' = (Get-Date $t[$i-2].Time)
'DeltaMins' = "{0:N2}" -f ((Get-Date $t[$i-1].Time) - (Get-Date $t[$i-2].Time)).TotalMinutes
}
}
}
}
}
$o | ft
InitiatingIdx InitiatingDtm DisconnectIdx DisconnectDtm ReconnectIdx ReconnectDtm DeltaMins
------------- ------------- ------------- ------------- ------------ ------------ ---------
24 3/31/2020 1:15:02 PM 23 3/31/2020 11:50:52 PM 22 3/31/2020 11:38:21 PM 12.52
22 3/31/2020 11:38:21 PM 21 3/31/2020 11:35:55 PM 20 3/31/2020 11:34:27 PM 1.47
20 3/31/2020 11:34:27 PM 19 3/31/2020 11:32:41 PM 18 3/31/2020 11:13:47 PM 18.90
18 3/31/2020 11:13:47 PM 17 3/31/2020 11:05:56 PM 16 3/31/2020 10:53:10 PM 12.77
16 3/31/2020 10:53:10 PM 15 3/31/2020 10:50:54 PM 14 3/31/2020 10:46:54 PM 4.00
14 3/31/2020 10:46:54 PM 13 3/31/2020 9:35:58 PM 12 3/31/2020 9:33:36 PM 2.37
12 3/31/2020 9:33:36 PM 11 3/31/2020 8:23:48 PM 10 3/31/2020 7:44:44 PM 39.07
9 3/31/2020 7:38:53 PM 8 3/31/2020 7:09:08 PM 7 3/31/2020 6:25:39 PM 43.48
7 3/31/2020 6:25:39 PM 6 3/31/2020 5:12:49 PM 5 3/31/2020 4:36:08 PM 36.68
5 3/31/2020 4:36:08 PM 4 3/31/2020 4:30:31 PM 3 3/31/2020 4:24:12 PM 6.32
2 3/31/2020 4:14:19 PM 1 3/31/2020 4:01:56 PM 0 3/31/2020 11:59:35 PM -477.65