PowerShell:将数组转换为 html table
PowerShell: convert array to html table
如何将 PowerShell 中的简单哈希数组转换为 HTML table?
$array = @{
"Name" = "My Name"
"Surname" = "My Surname"
"Address" = "My Address"
"DateOfBirth" = "My Date Of Birth"
"Hobby" = "My Hobby"
"Age" = "My Age"
}
然后继续添加行?以前有人实现过这个吗?下面我将根据几个在线论坛提供迄今为止我尝试过的示例:
[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
-Fragment
Cannot convert the "System.Collections.Hashtable" value of type
"System.Collections.Hashtable" to type
"System.Management.Automation.PSCustomObject". At line:0 char:0
+ [System.Management.Automation.PSCustomObject]$array | ConvertTo-Html ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
New-Object psobject -Property $array | ConvertTo-Html -Fragment
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
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
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
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
$table = $array.GetEnumerator() | select "Name", "Surname", "Address", "DateOfBirth", "Hobby", "Age" | ConvertTo-Html -Fragment -As Table
如您所见,有这么多不同的方法,其中 none 导致了成功:-(
'11','22','33' | ForEach{[PSCustomObject]@{'My Column Name'=$_}} | ConvertTo-HTML -片段-属性 'My Column Name'
你的输出将是:
<table>
<colgroup><col/></colgroup>
<tr><th>My Column Name</th></tr>
<tr><td>11</td></tr>
<tr><td>22</td></tr>
<tr><td>33</td></tr>
</table>
你的意思是这样的?
$table = [PSCustomobject]$array| ConvertTo-Html -Fragment -As Table
$table
<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>
您想从什么来源添加行?
如何将 PowerShell 中的简单哈希数组转换为 HTML table?
$array = @{
"Name" = "My Name"
"Surname" = "My Surname"
"Address" = "My Address"
"DateOfBirth" = "My Date Of Birth"
"Hobby" = "My Hobby"
"Age" = "My Age"
}
然后继续添加行?以前有人实现过这个吗?下面我将根据几个在线论坛提供迄今为止我尝试过的示例:
[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
-Fragment
Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Management.Automation.PSCustomObject". At line:0 char:0 + [System.Management.Automation.PSCustomObject]$array | ConvertTo-Html ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : ConvertToFinalInvalidCastException
New-Object psobject -Property $array | ConvertTo-Html -Fragment
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
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
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
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
$table = $array.GetEnumerator() | select "Name", "Surname", "Address", "DateOfBirth", "Hobby", "Age" | ConvertTo-Html -Fragment -As Table
如您所见,有这么多不同的方法,其中 none 导致了成功:-(
'11','22','33' | ForEach{[PSCustomObject]@{'My Column Name'=$_}} | ConvertTo-HTML -片段-属性 'My Column Name'
你的输出将是:
<table>
<colgroup><col/></colgroup>
<tr><th>My Column Name</th></tr>
<tr><td>11</td></tr>
<tr><td>22</td></tr>
<tr><td>33</td></tr>
</table>
你的意思是这样的?
$table = [PSCustomobject]$array| ConvertTo-Html -Fragment -As Table
$table
<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>
您想从什么来源添加行?