【问题标题】:How to sum only 2nd column values using powershell?如何使用powershell仅对第二列值求和?
【发布时间】:2013-06-23 13:32:48
【问题描述】:

sharesize.ps1

echo " "
$date1 = Get-Date
Write-Host -foreground Yellow -background Black "Script Started at $date1"

$path = "\*"

get-childitem $path | where {$_.PSIsContainer} | foreach { 

$size = (Get-ChildItem $_ -recurse | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 

$size = "{0:N2}" -f ($size / 1MB) + " MB"

$obj = new-object psobject 
add-member -inp $obj noteproperty Path $_.fullName 
add-member -inp $obj noteproperty "Size(MB)" $size 
[array]$report += $obj
}


#display the table
$a = "<style>"
$a = $a + "BODY{background-color:green;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;background-color:Yellow; font-family: Arial; font-size: 12pt}"
$a = $a + "TD{border-width: 2px;padding: 2px 6px 2px 3px;border-style: solid;border-color: black;background-color:Azure; font-family: Arial; font-size: 10pt}"
$a = $a + "</style>"


$report | Sort 'Size' -Descending | ConvertTo-HTML -head $a -title "Process Information" -body "<H2>Service Information</H2>"| Out-File -Append c:\temp\folder.html


$date2 = Get-Date
echo " "
Write-Host -foreground Yellow -background Black "Script Ended at $date2"
echo " "

上面的代码对我来说很好用,非常感谢下面的帮助。

这里我的要求是添加第 2 列卷的总和,并将输出附加到上述代码输出 html(c:\temp\folder.html) 的最后一行,


       Path                   | Size(MB)

 C:\NVIDIA\Displaydriver      |  400 MB
  *                           |  860 MB
  *                           |  100 MB
  *                           |   * MB
  *                           |   * MB

       Total                  |  1000 MB(sum of all numbers in 2nd column values)

我还需要将第二列值和总行对齐到 CENTER。

请帮忙

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-1.0


    【解决方案1】:

    总结大小这样做​​:

    $totalSize = ($report | Measure-Object 'Size(MB)' -Sum).Sum
    

    【讨论】:

    • 嗨,Keith,感谢您的重播,您能否详细说明您将上述 $totalsize 添加到的实际位置
    • 这有点作弊,但如果您想使用 ConvertTo-Html,请创建一个 Path 为“Total”且 Size 为总和的对象。将其添加为 $report 数组中的最后一个对象。但是,您必须以不同的方式进行排序,否则总数将出现在第一行。您可以在添加 Total 对象之前按大小对数组进行排序。
    【解决方案2】:
    $total = 0 
    $report | % {[float]$tmp = $_."Size(MB)".TrimEnd(" MB"); $total += $tmp}
    

    然后,您可以在 convertTo-html 和 bango 之前将 $total 对象添加到您的自定义 $report 对象中。感谢您提供简洁的脚本。

    唯一让我有点困惑的是你的 () 你的 $report 的 size 属性使 PS 认为它是一种方法,因此是引号。不是最好的约定,但它确实有效。

    更明确:

    ...
    [array]$report += $obj
    }
    
    $total = 0 
    $report | % {[float]$tmp = $_."Size(MB)".TrimEnd(" MB"); $total += $tmp}
    $obj = new-object psobject 
    add-member -inp $obj noteproperty Path "Total Size: "
    add-member -inp $obj noteproperty "Size(MB)" $total 
    [array]$report += $obj
    #display the table
    ...
    

    另外,删除 | Sort Size -Descending 以使总数显示在底部。

    【讨论】:

    • 嗨 Cole,感谢您的重播,能否请您给我完整的代码。
    • 编辑了我的帖子,可能想引用你从哪里得到你的代码;)
    【解决方案3】:

    我也有类似的需求,这是我的简单解决方案:

    $stuff=get-stuff
    $results=
        foreach ($item in $stuff) {
            $item | select column1,@{N="Column 2";E={$_.column2}},description,created,@{N="Size (GB)";E={"{0:N2}" -f $_.sizegb}}
        }
    
    $results_sorted=@($results | sort created)
    $results_sorted+=$item | select @{N="column1";E={"Totals"}},@{N="Column 2";E={$null}},@{N="Description";E={$null}},@{N="Created";E={$null}},@{N="Size (GB)";E={($results | Measure-Object "size (gb)" -Sum).Sum}}
    $results_sorted | ft column1,"Column 2",Description,Created,"Size (GB)"
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多