Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 " "

Above code is working great to me, help is much appreciated for the below help.

Here my requirement is to add sum of 2nd column volues and append the output to the last line of the above code output html(c:\temp\folder.html) as,


       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)

and also i need to align the 2nd column values and Total row to CENTER.

Please Help

share|improve this question

2 Answers

To sum the sizes do this:

$totalSize = ($report | Measure-Object 'Size(MB)' -Sum).Sum
share|improve this answer
 
Hi Keith, thanks for your replay, can you please elaborate to where actually you are adding the above $totalsize to –  user2523902 Jun 27 at 9:05
 
This is cheating a bit but if you want to use ConvertTo-Html, create an object whose Path is "Total" and Size is the sum. Add that as the last object in the $report array. However, you'll have to do your sorting a bit differently or the total will appear on the first line. You could sort the array on the size before you add the Total object. –  Keith Hill Jun 27 at 17:17
$total = 0 
$report | % {[float]$tmp = $_."Size(MB)".TrimEnd(" MB"); $total += $tmp}

Then you can just add the $total object to your custom $report object before you convertTo-html and bango. Thanks for the neat script.

Only thing that confused me a little was your () your size property of $report makes PS think its a method, thus the quotes. Is not the best convention, but it works.

More explicitly :

...
[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
...

Also, Remove | Sort Size -Descending for the total to appear on the bottom.

share|improve this answer
 
Hi Cole, thanks for your replay, can you please give me the entire code which worked for you. –  user2523902 Jun 27 at 9:06
 
Edited my post, might wanna cite where you got your code from ;) –  Cole9350 Jun 27 at 14:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.