Powershell - 帮助将哈希表转换为HTML

时间:2016-07-07 09:20:10

标签: html powershell hashtable

我已经按照创建哈希表并将其转换为HTML

的示例
http://stackoverflow.com/questions/19455435/converting-hashtable-in-powershell

以下是我的答案:

$Summary = @{
Computers=$ADServerCount.count
DMZ=$TotalDMZ.count
No_Ping=$TotalNoPing.count
Reg_Failed=$TotalRegFailed.count
RebootPending=$TotalPendingReboot.count
RebootNotRequired=$TotalNoPatchRequired.count 
}

[PSCustomObject]$Summary | Convertto-HTML -fragment

...如果我只是运行$ Summary,那就是结果 - 正是我的期望:

Name                           Value                                             
----                           -----                                             
RebootNotRequired              69                                                
DMZ                            10                                                
Computers                      341                                               
Reg_Failed                                                                       
RebootPending                  137                                               
No_Ping                        80 

但是,如果我尝试转换为HTML,则运行[PSCustomObject] $ Summary | Convertto-HTML -fragment,我得到的没有上面的值:

<table>
<colgroup>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
<col/>
</colgroup>
<tr><th>IsReadOnly</th><th>IsFixedSize</th><th>IsSynchronized</th><th>Keys</th>
<th>Values</th><th>SyncRoot</th><th>Count</th></tr>
<tr><td>False</td><td>False</td><td>False</td><td>System.Collections.Hashtable+
KeyCollection</td><td>System.Collections.Hashtable+ValueCollection</td><td>Syst
em.Object</td><td>1</td></tr>
</table>

请帮忙。感谢

以下是更多代码 - 正如您所看到的,还有两个可以转换为HTML表的变量,$ GroupPendingRebootbyOwner和$ OlderThan30Days,但$ Summary返回gobbledygook。我已经截断了发送电子邮件的其余代码。

$Summary = @{
Computers=$ADServerCount.count
DMZ=$TotalDMZ.count
No_Ping=$TotalNoPing.count
Reg_Failed=$TotalRegFailed.count
RebootPending=$TotalPendingReboot.count
RebootNotRequired=$TotalNoPatchRequired.count 
} 

$ExecSummary = [PSCustomObject]$Summary | Convertto-HTML -fragment

$GroupPendingRebootbyOwner = $AllTeamsFilter | Select Computer, RebootPending, Owner | Where {$_.Rebootpending -match 'True'} | 
Select Computer, Owner | Group Owner | Sort Count -desc | Select Name, Count | Convertto-HTML -fragment

$OlderThan30Days = $AllTeamsFilter | Select Computer, RebootPending, Owner, DaysAgo | 
Where {$_.RebootPending -match 'True' -and $_.DaysAgo -notmatch 'No_Ping' -and $_.DaysAgo -notmatch 'DMZ' -and $_.DaysAgo -notmatch 'Reg_Failed' -and $_.DaysAgo -gt '31'} | 
Select Computer, Owner, DaysAgo | Sort DaysAgo -desc | Convertto-HTML -fragment

$Mailto = "emailaddress"
$EmailFrom = "emailaddress"
$SMTPServer = "smtpserver"
$HTMLmessage = @"
<font color=""black"" face=""Arial"" size=""3"">
<h1 style='font-family:arial;'><b>Windows Updates and Pending Reboot Report</b></h1>
<style type=""text/css"">body{font: .8em ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;}
ol{margin:0 auto;}
table{width:auto;}
thead{}
thead th{font-size:120%;text-align:left;}
th{border-bottom:2px solid rgb(79,129,189);border-top:2px solid rgb(79,129,189);padding-bottom:10px;padding-top:10px;}
tr{padding:10px 10px 10px 10px;border:none;}
#middle{background-color:#900;}
</style>
<body BGCOLOR=""white"">
<p style='font: ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'><b>Executive Summary</b><br>
$ExecSummary
<p style='font: ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'><b>Pending Reboots by owner ("blank" name = unassigned owner)</b><br>
$GroupPendingRebootbyOwner
<p style='font: ""Lucida Grande"", Tahoma, Arial, Helvetica, sans-serif;'><b>Last rebooted more than 30 days ago</b><br>
$OlderThan30Days
</body>
"@

2 个答案:

答案 0 :(得分:4)

在PowerShell 2.0中,[PSCustomObject]$Hashtable语法不起作用。

使用New-Object cmdlet代替-Property parameter

New-Object psobject -Property $Summary |ConvertTo-Html -Fragment

答案 1 :(得分:3)

我认为对于以HTML形式查看哈希表,这应该得到更多答案。如果要获取哈希表,并将其放入电子邮件或其他任何内容的HTML中,则需要枚举每个条目的键和值。我相信此建议仅适用于Powershell V3及更高版本,但我可能是错的。因此:

PS > @{a=2;b=3;}

Name                           Value
----                           -----
a                              2
b                              3

也可以看作是:

PS > (@{a=2;b=3;}).GetEnumerator()

Name                           Value
----                           -----
a                              2
b                              3

然后可以通过转换(您可能不需要片段,但我经常从powershell在HTML中使用它):

PS > (@{a=2;b=3;}).GetEnumerator()| select Key,Value | ConvertTo-Html -Fragment
<table>
<colgroup><col/><col/></colgroup>
<tr><th>Key</th><th>Value</th></tr>
<tr><td>a</td><td>2</td></tr>
<tr><td>b</td><td>3</td></tr>
</table>