Powershell 导出到 csv - 动态日期范围

Powershell Export to csv - dynamic daterange

我有一个包含用户和日期时间的 csv

Users DHMS          
----- ----          
A     22/12/21 05:02:00
B     22/12/21 05:10:00
C     22/12/21 06:30:00
D     23/12/21 12:30:00
A     23/12/21 12:35:00
B     23/12/21 12:45:00
C     26/12/21 10:32:00
D     28/12/21 11:15:00
A     29/12/21 14:17:00
B     29/12/21 14:25:00

有没有一种简单的方法可以从每一行中提取一个新的 csv,其中的行在 -30 分钟 / + 30 分钟之间

我试着这样做

$culture = [Globalization.CultureInfo]::InvariantCulture
$Data = Import-Csv '.\test.csv' 

$Data | ForEach-Object {

    $DHMS = [DateTime]::ParseExact($($_.DHMS), 'dd/MM/yy HH:m:s', $culture)

    
$DHMSmoins = $DHMS.AddMinutes(-30)
$DHMSplus =  $DHMS.AddMinutes(30)


$path = ".\" + $($_.Users) +"\time.csv"


$Data | where { $DHMS  -ge $DHMSmoins  -and $DHMS -le $DHMSplus }
$Data | Export-Csv $path

}

但问题:

1/ daterange 导出错误

2/ 有多个用户,所以只导出最后一个用户的行。有没有办法在一个

中连接不同的 csv

希望 Tomalak 更清楚

在A文件夹中

的 csv
A     22/12/21 05:02:00
B     22/12/21 05:10:00
D     23/12/21 12:30:00
A     23/12/21 12:35:00
B     23/12/21 12:45:00
A     29/12/21 14:17:00
B     29/12/21 14:25:00

在B文件夹中

的 csv
A     22/12/21 05:02:00
B     22/12/21 05:10:00
D     23/12/21 12:30:00
A     23/12/21 12:35:00
B     23/12/21 12:45:00
A     29/12/21 14:17:00
B     29/12/21 14:25:00

在C文件夹中

的 csv
C     22/12/21 06:30:00
C     26/12/21 10:32:00

在D文件夹中

的 csv
D     23/12/21 12:30:00
A     23/12/21 12:35:00
B     23/12/21 12:45:00
D     28/12/21 11:15:00

根据评论完成任务:

one folder per user ("grouped by user") with a CSV file that contains all the events of that user, plus half an hour of "context" before and after each (independently of which user they belong to).

我会这样做:

$rows = Import-Csv '.\test.csv' -Delimiter ";" | foreach {
    [pscustomobject]@{
        date = [DateTime]::ParseExact($_.DHMS, 'dd/MM/yy HH:m:s', [Globalization.CultureInfo]::InvariantCulture)
        row = $_
    }
}

$rows | group { $_.row.Users } | foreach {
    $user = $_.Name
    $filename = ".$user\time.csv"

    # get context events (n.b.: this will contain duplicates!)
    $events = $_.Group | foreach {
        $start = $_.date.AddMinutes(-30)
        $until = $_.date.AddMinutes(30)
        ($rows | where { $_.date -ge $start -and $_.date -le $until }).row
    }

    # consolidate duplicates
    $index = @{}
    $uniqueEvents = foreach ($event in $events) {
        if (-not $index.ContainsKey($event)) {
            $index[$event] = $true
            $event
        }
    }

    Write-Host $filename -ForegroundColor Cyan
    Write-Host ($uniqueEvents | ConvertTo-Csv -Delimiter ";" -NoTypeInformation) -Separator "`r`n"  -ForegroundColor DarkCyan
    # $uniqueEvents | Export-Csv $filename -Delimiter ";" -NoTypeInformation
}

它为您的示例数据打印所需的输出:

.\A\time.csv
"Users";"DHMS"
"A";"22/12/21 05:02:00"
"B";"22/12/21 05:10:00"
"D";"23/12/21 12:30:00"
"A";"23/12/21 12:35:00"
"B";"23/12/21 12:45:00"
"A";"29/12/21 14:17:00"
"B";"29/12/21 14:25:00"
.\B\time.csv
"Users";"DHMS"
"A";"22/12/21 05:02:00"
"B";"22/12/21 05:10:00"
"D";"23/12/21 12:30:00"
"A";"23/12/21 12:35:00"
"B";"23/12/21 12:45:00"
"A";"29/12/21 14:17:00"
"B";"29/12/21 14:25:00"
.\C\time.csv
"Users";"DHMS"
"C";"22/12/21 06:30:00"
"C";"26/12/21 10:32:00"
.\D\time.csv
"Users";"DHMS"
"D";"23/12/21 12:30:00"
"A";"23/12/21 12:35:00"
"B";"23/12/21 12:45:00"
"D";"28/12/21 11:15:00"