kekimian
kekimian

Reputation: 947

ConvertTo-HTML output issue

I generate disk space usage HTML reports with gci and convertto-html. A part of my script generate 2 tables for each specified folder entered by -folder parameter. In the first table I retrieve the 10 biggest folders and in the second table I generate a file age report.

Now the problem: when only one directory is entered then the HTML output is fine, but if I enter 2 directory names I get only the tables for the last entered directory name.

For example:

my-function -folder c:\folder1, c:\folder2

will make only tables for the directory c:\folder2.

If I run this function without converting to HTML I get all tables for each directory in my console, same as I out-file to a simple txt.

Upvotes: 0

Views: 732

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

You get only the tables for the last folder in the list, because you assign the tables to variables inside the loop:

$folder | % {
    ...
    $10big = $gdfolders | ... | ConvertTo-Html -Fragment -As Table | Out-String
    $Fileage = $gdage | ... | ConvertTo-Html -Fragment -As Table | Out-String
}

ConvertTo-Html -Body "$10big $FileAge" | Out-File c:\test.html

This replaces the tables from the previous iteration with the ones from the current iteration, leaving you with just the tables from the last iteration after the loop finishes.

What you need to do is collect all tables in the loop by appending them to the variables (note the += inside the loop):

$10big = @()
$Fileage = @()
$folder | % {
    ...
    $10big += $gdfolders | ... | ConvertTo-Html -Fragment -As Table | Out-String
    $Fileage += $gdage | ... | ConvertTo-Html -Fragment -As Table | Out-String
}

ConvertTo-Html -Body "$10big $FileAge" | Out-File c:\test.html

Upvotes: 2

Related Questions