1

我有一个问题,我希望有人能够帮助...

我有以下代码:

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

Get-HTMLTable功能(如下图):

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
}

我遇到的问题:

我从中得到的输出如下:

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

但是我真正想要的输出如下:

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

我认为我做错的部分代码是我没有得到正确输出的原因是因为这几行和 foreach 对象循环:

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

我确定我忽略了一些明显的东西,但看不到我做错了什么。

非常感谢您的帮助,谢谢

4

1 回答 1

1

我也怀疑这与您如何在 foreach-object 循环中构建自定义对象有关;玩了一段时间后,我放弃了,尝试从头开始构建它。希望这对你有用。

在您的 Get-ChildItem 调用之前创建一个空数组:

$tableObject = @()

然后更改您的 foreach-object 块以使用 psobject 的内容迭代地填充数组:

    ForEach-Object {

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

最后,在 foreach-object 块之外添加表,传递数组而无需选择任何内容:

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

根据我的评论,不要更改数组中的值,用正确的值填充数组:

    ForEach-Object {

        $properties = "" | Select 'Whatever You', Might, Want
            $properties.('Whatever You') = $_.Directory 
            $properties.Might = $_.Name 
            $properties.Want = $_.LastWriteTime
            $tableObject += $properties
    } 
于 2013-08-21T18:49:06.393 回答