Powershell,来自文本文件的总和

时间:2016-02-16 18:00:49

标签: powershell text sum

我有一个脚本,可以创建文本文件中列出的每月总日常使用量。 每日每日统计档案都采用以下格式:

location,Feeder,total,endpoints
Oaklake,1,11153,310
oaklake,2,26214,291
oaklake,3,4593,147
oaklake,4,5279,145

以下是我遇到问题的脚本的相关部分:

#create list of last month's days
if ((get-date).day -eq 2) {
    $fullmonth = "location,Feeder,Usage,endpoints`n"
    $year = (get-date).addmonths(-1).year
    $month = (get-date).addmonths(-1).month
    $days = 1..[datetime]::daysinmonth($year,$month) |
        %{(get-date -day $_ -month $month -year $year).toshortdatestring()}

#select the file for each particular day and add it's content to $fullmonth
foreach ($day in $days) {
    $dayfile = ls "c:\powershell\locationUse\summaries" |
        ?{$_.creationtime -gt $day -and $_.creationtime -lt $(get-date $day).adddays(1).toshortdatestring()} |
        sort creationtime | select -last 1 | gc | select -skip 1
    $fullmonth += $($dayfile | out-string)
}
$fullmonth | export-csv ./fullmonthtest.csv -notypeinformation

我的问题是fullmonthtest.csv的最终输出是摘要的最后一天的重复 文件,如:

location,Feeder,total,endpoints
Oaklake,1,11153,310
oaklake,2,26214,291
oaklake,3,4593,147
oaklake,4,5279,145
Oaklake,1,11153,310
oaklake,2,26214,291
oaklake,3,4593,147
oaklake,4,5279,145
Oaklake,1,11153,310
oaklake,2,26214,291
oaklake,3,4593,147
oaklake,4,5279,145
Oaklake,1,11153,310
oaklake,2,26214,291
oaklake,3,4593,147
oaklake,4,5279,145

我使用" + ="方法不正确或什么?感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

没关系。我发现这个月的文件内容在每个文件中是相同的。我的脚本工作正常,我只是没有验证我的数据来源。这就是我使用从当前脚本的旧版本创建的数据所获得的。哎呀!感谢Mathias的回复。