convertto-html help

by penguinviw at 2013-04-03 08:32:06

I have the following script that works if I dont try to convertto-html. I have tried using the out-file, set-content and the > to designate the output file, but none work. It creates the html file, but puts random numbers for the information in it or it puts repeated "Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData". Here is my code:
Import-Module ActiveDirectory
if(!(Get-PSSnapin |
Where-Object {$.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) {
ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}

$ErrorActionPreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"

$oulist = Get-OrganizationalUnit "KT" -SingleNodeOnly
$HtmlReport = @()

ForEach ($Ounames in $Oulist){
$Results = @()
ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $Ounames.Name)){
$Results += Get-mailboxstatistics $Mailbox | Select @{label="User";expression={$
.DisplayName}}, <br> @{label=&quot;TotalSizeMB&quot;;expression={$_.TotalItemSize.Value.ToMB&#40;&#41;}},
@{label="Items";expression={$.ItemCount}}, <br> @{label=&quot;Storage Limit&quot;;expression={$_.StorageLimitStatus}},
@{name="PrimarySMTPAddress";expression={$mailbox.PrimarySMTPAddress}}, `
Database,ServerName
}
$Html1 = $Html1 += $Results | Where { $
-ne $null } | Sort-Object TotalSizeMB -Descending | ft -Autosize
$Html2 = $Html2 += ($Results | Measure-Object -Property 'TotalSizeMB' -Sum).Sum

}
$HtmlReport = $Html1 + $Html2
$HtmlReport = $HtmlReport | ConvertTo-Html -fragment | OUt-File C:\HtmlReport.html
Invoke-Expression C]
Thanks so much!!!
by MasterOfTheHat at 2013-04-03 08:58:49
The ConvertTo-Html cmdlet "Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser." so you need to pass it the object you want in the html. By piping your $Results line to the Format-Table cmdlet, (line 23), you are converting the object into a stream of formatting directives, (see this really old post by Jeff Snover). So, leave out the "| ft -Autosize" part of that line and see what you get. I’m pretty sure you’re going to lose the $Html2 value, though… Grab the ebooks on the TechLetter page and see what Don has to say in his Creating HTML Reports in PowerShell book.

As an aside, lines 23 and 24 each has redundant syntax.
$Html1 = $Html1 += $Results…
The += operator actually means "add the value on the right side of this operator to what is on the left side of this operator and then assign the new value to what is on the left side of the operator." So you could just use:
$Html1 += $Results…
by penguinviw at 2013-04-03 09:55:54
Awesome. Removing the "|ft" now gets me meaningful results.
You are right about loosing the $Html. I really need that calculation per OU. How can I get it into the report?
Thanks again.
by MasterOfTheHat at 2013-04-03 13:02:56
You can always tack it onto the end of your HTML. (FYI, this a really rough example and should be polished.)
# Do a bunch of stuff to create the $Results object

# Filter the object, sort it, convert it to an HTML table, and assign it to a variable
$Html1 = $Results | Where { $_ -ne $null } | Sort-Object TotalSizeMB -Descending | ConvertTo-Html -fragment
# Add a new DIV element to the HTML that contains the sum of the TotalSizeMB column in the $Results object
$Html1 += "<div>$(($Results | Measure-Object -Property 'TotalSizeMB' -Sum).Sum)</div>"
# Spit out all of the HTML
$Html1
by penguinviw at 2013-04-04 10:54:42
I think I got it. Thanks.