1

I have a problem I am hoping someone is able to help with...

I have the following code:

Else {
                $script:MyReport += Get-CustomHeader "2" "Files older than"

                Get-ChildItem -Path $Target -Recurse | Where-Object { $_.LastWriteTime -lt $(get-date).('Add' + $PeriodName).Invoke(-$periodvalue) `
                -and $_.psiscontainer -eq $false } | `
                #Loop through the results and create a hashtable containing the properties to be added to a custom object

                ForEach-Object {
                    $properties = @{ 
                        Path = $_.Directory 
                        Name = $_.Name 
                        DateModified = $_.LastWriteTime }
                    #Create and output the custom object
                    $var1 = New-Object PSObject -Property $properties | select Path,Name,DateModified  
                    $script:MyReport += Get-HTMLTable ( $var1 | select Path,Name,DateModified )   

                }             

       } #Close Else clause on Test-Path conditional

The Get-HTMLTable function (shown below):

Function Get-HTMLTable{
    param([array]$Content)
    $HTMLTable = $Content | ConvertTo-Html
    $HTMLTable = $HTMLTable -replace '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', ""
    $HTMLTable = $HTMLTable -replace '<html xmlns="http://www.w3.org/1999/xhtml">', ""
    $HTMLTable = $HTMLTable -replace '<head>', ""
    $HTMLTable = $HTMLTable -replace '<title>HTML TABLE</title>', ""
    $HTMLTable = $HTMLTable -replace '</head><body>', ""
    $HTMLTable = $HTMLTable -replace '</body></html>', ""
    Return $HTMLTable
}

The problem I am having:

The output I get from this is as follows:

Path                Name             DateModified
\\10.0.0.1\folder1  document1.xls    19/08/2013 16:02:01
Path                Name             DateModified
\\10.0.0.1\folder1  test.doc     20/08/2013 15:47:06
Path                Name             DateModified
\\10.0.0.1\folder1  anotherfile.txt  20/08/2013 15:42:26

However the output I actually want is as follows:

Path                Name             DateModified
\\10.0.0.1\folder1  document1.xls    19/08/2013 16:02:01
\\10.0.0.1\folder1  test.doc     20/08/2013 15:47:06
\\10.0.0.1\folder1  anotherfile.txt  20/08/2013 15:42:26

The part of the code I believe I am doing incorrectly and is the reason I am not getting the correct output is because of these few lines and the foreach object loop:

$var1 = New-Object PSObject -Property $properties | select Path,Name,DateModified  
$script:MyReport += Get-HTMLTable ( $var1 | select Path,Name,DateModified )   

Im sure there is something obvious I am overlooking but cannot see what I am doing wrong.

Your help with this much appreciated, thanks

1
  • Have a look at the HTML source output ? It depends on ConvertTo-Html, but seems it is appending an entire table for each line. You could split up Get-HTMLTable into 3 functions: get-htmlTableHeader (print everything until </th>), get-htmlTableRow (print <tr> until </tr>) and get-htmlTableFooter (starting from </table>). You'd have to call get-htmlTableHeader and get-htmlTableFooter outside the loop. Commented Aug 21, 2013 at 17:27

1 Answer 1

1

I too suspect it's related to how you're building the custom object within the foreach-object loop; after playing a while I gave up and tried to build it from scratch. Hopefully this will work for you.

Before your Get-ChildItem call create an empty array:

$tableObject = @()

Then change your foreach-object block to fill the array iteratively with the contents of the psobject:

    ForEach-Object {

        $properties = "" | Select Directory, Name, DateModified
            $properties.Directory = $_.Directory 
            $properties.Name = $_.Name 
            $properties.DateModified = $_.LastWriteTime
            $tableObject += $properties
    }             

Finally, outside of the foreach-object block add the table, passing the array without having to select anything:

$script:MyReport += Get-HTMLTable ( $tableObject )

As per my comment, don't change the values in the array, populate the array with the correct values:

    ForEach-Object {

        $properties = "" | Select 'Whatever You', Might, Want
            $properties.('Whatever You') = $_.Directory 
            $properties.Might = $_.Name 
            $properties.Want = $_.LastWriteTime
            $tableObject += $properties
    } 
Sign up to request clarification or add additional context in comments.

6 Comments

Excellent, yes this successfully worked. I am now trying to change the headings that appear in the HTML table that is created. I thought replacing the values in the array with the ones I want would work ie `$tableObject = $tableObject -replace 'Name','ReplacementText', which does change the "Name" in the array to "ReplacementText". But when I call the Get-HTMLTable function with the new array and it prints the table it just prints a few numbers rather than the new table i want with the "Name" heading changed to "ReplacementText". What is the correct way to go about doing this? thanks a lot
Probably best to change the custom object properties to what you want them called rather than renaming them later. Change the name of the note properties on the $properties PSCustomObject to whatever you want, just make sure to reference them correctly when filling them! Descriptive code above.
Excellent, thanks...That was the way I thought it should be done, but where I was going wrong was not changing the "" | Select Whatever, You, Want part as well. If I wanted the replacement text to contain spaces as well ie instead of "Name" it would say "This is the name", what is the correct syntax to use when changing the $property part because obviously $properties.This is the name = $_.Name will not work? thansk for your help
Not sure if this is the 'correct' syntax but this works for me (see amended answer).
Weird, I tested that under version 2 & 3 and version 3 ISE and it worked fine in all... are you sure your code is updated correctly? Properties with spaces should be "quoted" - maybe try without the braces? $properties."Whatever You"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.