假如$Total的查询结果为如下所示:
我想根据 ExecutionTime ($Total.ExecutionTime -eq $((Get-Date).AddDay(-7).ToString(‘yyyy/MM/dd’)))进行分列判断,并汇总成如下格式,还请大神帮助,非常感谢!!!
P.S.
使用New-Object -Type PSObject -Property 只会转一列,使用foreach 循环感觉太复杂,还请大神们帮帮忙,非常感谢!!!
1 Answers
Best Answer
兄弟,提个建议,所有非通用型的需求或者功能,都得自己动手,哪怕写100个循环,只要能达到目的,也比什么都不写有意义,加油。
这是我的数据:
ServerName,Location,Total,ExecutionTime Pstips.NET,ShangHai,1,2018-07-31 Pstips.NET,Beijing,2,2018-08-01 Pstips.NET,ShangHai,3,2018-08-02 Pstips.NET,Beijing,4,2018-08-03 Pstips.NET,ShangHai,5,2018-08-04 Pstips.NET,Beijing,6,2018-08-05 Pstips.NET,ShangHai,7,2018-08-06 Pstips.NET,Beijing,8,2018-08-07
处理的脚本:
#星期常量 $weeks = @('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday') #导入报表,进行处理 Import-Csv D:\report.csv | foreach { $date = [datetime]($_.ExecutionTime) $Key = "{0},{1}" -f $_.ServerName,$_.Location $_ | Add-Member -MemberType NoteProperty -Name 'Week' -Value $date.ToString("dddd") $_ | Add-Member -MemberType NoteProperty -Name 'Key' -Value $Key $_ } | Group-Object Key | ForEach-Object { $g = $_ $obj = [PSCustomObject]@{ ServerName = $_.Group[0].ServerName Location = $_.Group[0].Location } foreach($w in $weeks){ $total = ($g.Group | where { $_.Week -eq $w } | Measure-Object -Sum Total).Sum if($total -eq $null){ $total = 0 } $obj | Add-Member -MemberType NoteProperty -Name $w -Value $total } $obj } | Format-Table
输出如下:
ServerName Location Monday Tuesday Wednesday Thursday Friday Saturday Sunday ---------- -------- ------ ------- --------- -------- ------ -------- ------ Pstips.NET ShangHai 7 1 0 3 0 5 0 Pstips.NET Beijing 0 8 2 0 4 0 6
您的思路和逻辑,让我学到了很多!非常感谢Mooser老大的帮助和提醒!
您的思路和逻辑,让我学到了很多!非常感谢Mooser老大的帮助和提醒!