从Powershell的多个日期戳中获取持续时间



我有两组数据,我正试图使用PowerShell操作它们以获得预期的输出。关于我该如何做到这一点,有什么想法吗?感谢提前提供的帮助

数据1:

StartTime                      Url
2022-01-28T04:44:28.111542Z    Url a
2022-02-02T13:35:15.1040997Z   Url b
2022-02-02T15:33:57.961112Z    Url c

数据2:

Endtime                         Url
2022-01-28T22:11:39.1086189Z    Url m
2022-02-04T02:49:08.6644804Z    Url d

目标在下方的表格中

Startime                     Endtime                        Url of Starttime    duration
2022-01-28T04:44:28.111542Z 2022-01-28T22:11:39.1086189Z    Url a   
2022-02-02T15:33:57.961112Z 2022-02-04T02:49:08.6644804Z    Url c   

如果您的输入数据集是powershell对象数组

你可以做:

$result = foreach ($item in $data1) {
$matchingItem = $data2 | Where-Object { $_.Url -eq $item.Url }
if ($matchingItem) {
# choose the resolution of the duration. Here I'm using TotalSeconds
# but you can also opt for TotalDays, TotalHours, TotalMinutes or TotalMilliseconds
$duration = ([datetime]$matchingItem.EndTime - [datetime]$item.StartTime).TotalSeconds  
# return a new object with properties combined
[PsCustomObject]@{
StartTime            = $item.StartTime
EndTime              = $matchingItem.EndTime
Url                  = $item.Url
'Duration (Seconds)' = $duration
}
}
}
# show on screen
$result | Format-Table -AutoSize
# save as new CSV
$result | Export-Csv -Path 'X:SomePathdurations.csv' -NoTypeInformation

屏幕输出:

StartTime                   EndTime                      Url   Duration (Seconds)
---------                   -------                      ---   ------------------
2022-01-28T04:44:28.111542Z 2022-01-28T22:11:39.1086189Z Url a      62830.9970769
2022-02-02T15:33:57.961112Z 2022-02-04T02:49:08.6644804Z Url c     126910.7033684

最新更新