Windows Vista Forums

Output to HTML

  1. #1


    Monkey Guest

    Output to HTML

    I have a script that checks a text file for folder paths and then calculates
    the folder sizes and amount of items. I had it originally outputting to CSV
    but wanted to make it output to HTML. I have got it doing this, but it
    doesn't look very tidy. I also wondered if it is possible to have the output
    display red if the number of items are above 10,000. It also seems to take a
    while to run and wondered if anyone could help?

    Get-Content C:\folders.txt `
    | ForEach-Object {
    $stats = Get-ChildItem $_ -Force -Recurse | Measure-Object -Property
    Length -Sum
    New-Object PSObject `
    | Add-Member -MemberType NoteProperty -Name Path -Value $_
    -PassThru `
    | Add-Member -MemberType NoteProperty -Name NumItems -Value
    $stats.Count -PassThru `
    | Add-Member -MemberType NoteProperty -Name Size -Value
    ('{0:0.##} GB' -f ($stats.Sum / 1GB)) -PassThru
    } `
    | ConvertTo-HTML name, path, NumItems, Size -title "Exchange Logs" |
    Set-Content C:\inetpub\wwwroot\PS-exchangelogs.html


      My System SpecsSystem Spec

  2. #2


    Chris Dent Guest

    Re: Output to HTML


    I can't tell you much about the run-speed, it depends a lot on the
    number of folders you're testing and how many items hide in there.

    Anyway, you can do a hell of a lot more with the style than I have here,
    but this would seem like a reasonable start (w3schools.com is a fine
    place to start if you want to learn basic HTML / CSS):


    # A style to insert into the Html Head
    $Head = "
    <title>Exchange Logs</title>
    <style type='text/css'>
    td.Red { color: Red }
    </style>"

    # Generate the base Html
    $Html = Get-Content C:\Folders.txt | %{
    # Get the size and count
    $Stats = Get-ChildItem $_ -Force -Recurse | Measure-Object Length -Sum

    # Construct an object the old-fashioned way
    $_ | Select-Object `
    @{n='Path';e={ $_ }},
    @{n='NumItems';e={ $Stats.Count }},
    @{n='Size';e={ '{0:0.##} GB' -f ($Stats.Sum / 1GB) }}

    } | ConvertTo-Html -Head $Head

    # Match and replace NumItems over 10000
    $Html = $Html | %{
    # Match from the beginning of the line, on the second
    # field (NumItems), and catch digit strings 5 or more long
    If ($_ -Match "^<tr><td>.*</td><td>\d{5}") {
    # Add the class tag for the TD element on this row
    $_ -Replace "<td>", "<td class='Red'>"
    } Else {
    # Otherwise leave the line unmodified
    $_
    }
    }

    # Write all of it back to a file
    $Html > "c:\inetpub\wwwroot\PS-exchangelogs.html"

      My System SpecsSystem Spec

  3. #3


    Chris Dent Guest

    Re: Output to HTML

    Chris Dent wrote:

    >
    > I can't tell you much about the run-speed, it depends a lot on the
    > number of folders you're testing and how many items hide in there.
    >
    > Anyway, you can do a hell of a lot more with the style than I have here,
    > but this would seem like a reasonable start (w3schools.com is a fine
    > place to start if you want to learn basic HTML / CSS):
    >
    > $Snip
    Just to be clear, all you need do to make the output pretty is play with
    the HTML.

    You might extend the style above rather a lot. For example, you might
    set a default font, and you might align to the left, or you might make
    the table fixed size.

    If you want the table contents to be sorted on size / number of items I
    suggest you do that before you convert to HTML

    Here's an extended CSS style example to get you started off:


    # A style to insert into the Html Head
    $Head = "
    <title>Exchange Logs</title>
    <style type='text/css'>
    table { border-collapse: collapse; width: 700px }
    body { font-family: Arial }
    td, th { border-width: 2px; border-style: solid; text-align: left;
    padding: 2px 4px; border-color: black }
    th { background-color: grey }
    td.Red { color: Red }
    </style>"

      My System SpecsSystem Spec


Output to HTML
Similar Threads
Thread Forum
Formating win32_bios output as HTML PowerShell
Enexpected output when using output-csv PowerShell
working on html objects using HTML DOM, VBscript VB Script
Formatting HTML output possible? PowerShell
Better HTML-ized output to file unicode/ascii thing PowerShell