2013-08-21 31 views
1

我有我希望有人能够帮助...一个问题的Windows PowerShell - 无法创建一个HTML表格正确

我有以下代码:

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) 

我确定有一些显而易见的东西可以忽略,但是看不到我做错了什么。

你的帮助,这非常赞赏,感谢

+0

看一看HTML源代码输出?它依赖于ConvertTo-Html,但似乎它为每行添加了一个整个表格。你可以将Get-HTMLTable分成3个函数:get-htmlTableHeader(打印一切,直到),get-htmlTableRow(打印,直到)和get-htmlTableFooter(从开始)。你必须在循环之外调用get-htmlTableHeader和get-htmlTableFooter。 –

回答

1

我也怀疑这是关系到你是如何构建的foreach循环对象中的自定义对象;玩了一段时间后,我放弃了,并试图从头开始构建它。希望这会对你有用。

的GET-ChildItem调用之前创建一个空数组:

$tableObject = @() 

然后更改您的foreach对象块,填补了数组迭代与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 
    } 
+0

非常好,是的,这成功地工作。我正在尝试更改出现在创建的HTML表中的标题。我想用数组中的值替换数组中的值,例如'$ tableObject = $ tableObject -replace'Name','ReplacementText',它将数组中的“Name”更改为“ReplacementText”。但是,当我用新数组调用Get-HTMLTable函数并打印表格时,它只是打印几个数字,而不是我想要的“新名称”标题更改为“ReplacementText”的新表格。这样做的正确方法是什么?非常感谢 –

+0

可能最好将自定义对象属性更改为您希望它们调用的内容,而不是稍后重命名它们。将$ properties PSCustomObject中的音符属性的名称更改为任何你想要的,只要确保在填充它们时正确引用它们即可!上面的描述性代码。 – nimizen

+0

非常好,谢谢......这是我认为应该完成的方式,但是我错误的地方并没有改变''“|选择Whatever,You,Want'部分。如果我希望替换文本包含空格,而不是“名称”,它会说“这是名称”,更改'$ property'部分时使用的正确语法是什么,因为显然是'$ properties.This名称= $ _。名称'不起作用? thansk求助 –