PowerShell Convertto-HTML将数据添加到列

时间:2013-04-24 15:35:48

标签: html powershell scripting powershell-v2.0

我有这个脚本

$a = "<style>"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color:black;}"
$a = $a + "Table{background-color:#ffffff;border-collapse: collapse;}"
$a = $a + "TH{border-width:1px;padding:0px;border-style:solid;border-color:black;}"
$a = $a + "TR{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "TD{border-width:1px;padding-left:5px;border-style:solid;border-color:black;}"
$a = $a + "</style>"


dir -path  "\\server\loctation$"  |where {$_.mode -match "d"} | select-object  name, creationtime, lastwritetime, Owner| ConvertTo-Html -head $a | Out-File "\\server\location\drivelistings.html"

输出结果很好。

但是我想在表的末尾添加一个列,其中包含前缀数据。

目前页面输出:

   Name   Creation Time    LateWriteTime   Owner
   Test   2013/04/12       2013/04/12
   Test2  2013/04/12       2013/04/12
   Test3  2013/04/12       2013/04/12

我想使用成员列手动输入所有者。此数据不会更改,文件夹结构也不会更改。

例如:

   Name   Creation Time    LateWriteTime   Owner
   Test   2013/04/12       2013/04/12      user1
   Test2  2013/04/12       2013/04/12      user2
   Test3  2013/04/12       2013/04/12      user3

有什么办法吗?

1 个答案:

答案 0 :(得分:0)

如果你可以创建一个csv,将所有者映射到文件夹名称,完整路径或其他独特的东西,你可以试试这个:

文件夹用户mappings.csv

"FolderName","Owner"
"Test","user1"
"Test2","user2"
"Test3","user3"

脚本

$a = Import-Csv folder-user-mappings.csv
#Convert to hashtable to simplify code and search faster
$mappings = @{}
$a | % { $mappings.Add($_.FolderName, $_.Owner) }

dir -path  "\\server\loctation$" | 
where {$_.mode -match "d"} |
select-object name, creationtime, lastwritetime, @{n="Owner";e={$mappings[$_.Name]}} | 
ConvertTo-Html -head $a | 
Out-File "\\server\location\drivelistings.html"