PowerShell:将数组转换为html表

时间:2018-11-12 18:43:39

标签: 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" 
 }

enter image description here

然后继续添加行?有人做到过吗?下面,我将根据几个在线论坛提供到目前为止我已经尝试过的示例:

[System.Management.Automation.PSCustomObject]$array | ConvertTo-Html
-Fragment
  

无法转换类型的“ System.Collections.Hashtable”值   键入“ System.Collections.Hashtable”   “ System.Management.Automation.PSCustomObject”。在第0行char:0   + [System.Management.Automation.PSCustomObject] $ array |转换为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

enter image description here

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

enter image description here

如您所见,有很多不同的方法,但是没有一种方法可以成功:-(

2 个答案:

答案 0 :(得分:1)

您的意思是这样的吗?

$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>

您想从什么来源添加行?

答案 1 :(得分:0)

'11','22','33'| ForEach {[PSCustomObject] @ {'我的列名'= $ _}} | ConvertTo-HTML -Fragment -Property“我的列名”

您的输出将是:

library(tidyverse)
df1 %>%
   group_by(OFFENCE_CODE, YEAR, MONTH) %>%
   mutate(SUM_NO_OF_CRIME = sum(NO_OF_CRIME))