将通过Get-ChildItem获得的多个对象写入HTML

时间:2013-11-11 16:54:09

标签: powershell

我目前正在尝试编写一个脚本,该脚本将创建位于给定目录中的.txt和.pdf文件列表,其中包含有关它的一些信息。使用Get-ChildItem我可以得到我想要的结果,但是我无法将其转换为HTML,因为在这样做时我会丢失有关该文件的所有信息(仅保留文件的名称)。使用此代码,我只能获取最后一个文件所需的信息:

$types = @("*.txt", "*.pdf")
foreach ($type in $types) {
  Get-ChildItem C:\POWERSHELL -Filter $type -Recurse | ConvertTo-Html |
  Out-File c:\status23.html
}

使用此代码我只会收到名称:

$result = {
  foreach ($type in $types) {
    Get-ChildItem C:\POWERSHELL -Filter $type 
  }
}

ConvertTo-HTML -Body "$result" -Title "htmlname" | Out-File c:\status343t4.html

希望有人能帮助我 - 我怎样才能获得所有需要的信息?

1 个答案:

答案 0 :(得分:2)

如上所述,您的第一个示例将使用PDF列表覆盖TXT列表 - 可能不是您想要的。

相反,收集所有文件,然后有选择地将您想要的属性输出到HTML。

$types=@("*.txt", "*.pdf")
$myfiles = @();
foreach($type in $types){
    $myFiles += Get-ChildItem C:\POWERSHELL -Filter $type -Recurse 
}

$myfiles | ConvertTo-Html -property fullname,lastwritetime,length | out-file c:\status23.html;