将包含在字符串中的表转换为HTML

时间:2012-09-05 09:41:39

标签: html email powershell

我有一张由各种工作项目制成的桌子。这是表格布局:

$table = @{Expression = {$_.Fields['Id'].Value}; Label = "Id"}, 
@{Expression = {$_.Fields['Work Item Type'].Value}; Label = "Work Item Type"}, 
@{Expression = {$_.Fields['Title'].Value}; Label = "Title"}, 
@{Expression = {$_.Fields['State'].Value}; Label = "State"},
@{Expression = {$_.Fields['Substate'].Value}; Label = "Substate"}, 
@{Expression = {$_.Fields['Assigned To'].Value}; Label = "Assigned To"}, 
@{Expression = {$_.Fields['CreatedBy'].Value}; Label = "Developer"},
@{Expression = {$_.Fields['QA Responsible'].Value}; Label = "QA Responsible"},
@{Expression = {$_.Fields['Iteration Path'].Value}; Label = "Iteration Path"}

从这行代码$workItems | Format-Table $table | Out-String我得到一个这样的表:

         Id Work Item Type  Title          State          Substate       Assigned To    Developer      QA Responsible    Iteration Path
         -- --------------  -----          -----          --------       -----------    ---------      -----------    --------------
      38479 Bug             Title1000000...Resolved       zzzzzzzzzzz... xxxxxxxxxxx... zzzzzzzzzzz... zzzzzzzzzzz     QA\2012 August
      34917 Task            Title2000000...Resolved       zzzzzzzzzzz... xxxxxxxxxxx                                  WebXXX\CCCCCC 

但是当我尝试将其转换为HTML时,我会得到不同的值。像这里一样:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup>
<col/>
</colgroup>
<tr><th>*</th></tr>
<tr><td>305</td></tr>
</table>
</body></html>

我只想在HTML格式化表格并通过电子邮件发送。我可以得到任何帮助吗?

2 个答案:

答案 0 :(得分:1)

将管道移至Out-String cmdlet,然后重试,Out-String将对象转换为字符串:

$workItems | Format-Table $table | ConvertTo-Html

答案 1 :(得分:0)

好的,在Shay的帮助下,我明白了。如果有人遇到类似的问题,我会在这里发布答案。修复程序就在此行$workItems | Format-Table $table | ConvertTo-Html。需要将其更改为$workItems | Select-Object $table | ConvertTo-Html

谢谢你帮了我很多次,如果你碰巧在萨拉热窝,我会给你买一杯饮料:)