PowerShell:将数组转换为 html 表

Tir*_*ing 1 html powershell converters

如何将 PowerShell 中的简单哈希数组转换为 HTML 表?

$array = @{

"Name" = "My Name"
"Surname" = "My Surname"
"Address" = "My Address"
"DateOfBirth" = "My Date Of Birth"
"Hobby" = "My Hobby"
 "Age" = "My Age" 
 }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

然后继续添加行?有没有人以前实现过这个?下面我将根据几个在线论坛提供我迄今为止尝试过的示例:

[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
-Fragment
Run Code Online (Sandbox Code Playgroud)

无法将“System.Collections.Hashtable”类型的“System.Collections.Hashtable”值转换为“System.Management.Automation.PSCustomObject”类型。在 line:0 char:0 + [System.Management.Automation.PSCustomObject]$array | ConvertTo-Html ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException +fullyQualifiedErrorId : ConvertToFinalInvalidCastException

New-Object psobject -Property $array | ConvertTo-Html -Fragment
Run Code Online (Sandbox Code Playgroud)

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -Fragment
Run Code Online (Sandbox Code Playgroud)

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

 $array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment
Run Code Online (Sandbox Code Playgroud)

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$array | Select 'Name','Surname','Address','DateOfBirth','Hobby', 'Age' | ConvertTo-HTML -as Table -Fragment | Out-String
Run Code Online (Sandbox Code Playgroud)

System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry System.Collections.DictionaryEntry

$table = $array.GetEnumerator() | ConvertTo-Html -Fragment -As Table
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

$table = $array.GetEnumerator() | select "Name", "Surname", "Address", "DateOfBirth", "Hobby", "Age" | ConvertTo-Html -Fragment -As Table
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

正如你所看到的,有这么多不同的方法,但没有一个能成功:-(

小智 6

你的意思是这样的?

$table = [PSCustomobject]$array| ConvertTo-Html -Fragment -As Table
$table
Run Code Online (Sandbox Code Playgroud)
<table>
<colgroup><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>Name</th><th>Age</th><th>Surname</th><th>DateOfBirth</th><th>Hobby</th><th>Address</th></tr>
<tr><td>My Name</td><td>My Age</td><td>My Surname</td><td>My Date Of Birth</td><td>My Hobby</td><td>My Address</td></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

您想从哪个来源添加行?