powershell - 如何使用powershell仅对第二列值求和?

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

共享大小.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 " "

上面的代码对我很有用,非常感谢以下帮助。
这里我的要求是添加第二列卷的总和并将输出附加到上述代码输出 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)

并且我还需要将第二列值和总行对齐到中心。
请帮忙

最佳答案

总结尺寸这样做:

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

关于powershell - 如何使用powershell仅对第二列值求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319600/

相关文章:

date - 怎么把Get-WinEvent的TimeCreated转换成yyyy-MM-dd-hh-mm-ss格式?

powershell - MS 的哪些 PowerShell cmdlet 支持 -UseTransaction

powershell - PowerShell 中的动态参数问题

exception - 如何捕获另一个 Powershell 脚本中抛出的异常?

powershell - 加载模块时未处理 list

powershell - 从日志文件中删除前 N 行的最有效方法

powershell - 了解Powershell : example - Convert JSON to CSV

powershell - 在PowerShell中使用换行符连接数组

PowerShell - 分布式解决方案

powershell - 是否可以通过参数绑定(bind)使别名成为全局的?