【发布时间】:2014-01-08 05:07:32
【问题描述】:
我每天都在检查 Hyper-V 虚拟机,并根据需要获得 HTML 格式的输出。 但是,html 格式总是有改进的余地,我需要帮助解决以下问题。
我有以下数组
$outputArray = @()
foreach($VM in $VMS) {
$VMsRAM = [math]::round($VM.Memoryassigned/1GB)
$VMsCPU = $VM.processorCount
$VMsState = $VM.State
$VMsStatus = $VM.Status
$VMsUptime = $VM.Uptime
$VMsAutomaticstartaction = $VM.Automaticstartaction
$VMsIntegrationServicesVersion = $VM.IntegrationServicesVersion
$VMsReplicationState = $VM.ReplicationState
$VHDsGB = @{ label="File_Size"; Expression={[math]::round($_.FileSize/1GB)}}
$VHDs = Get-VHD -ComputerName $VM.ComputerName -VMId $VM.Id | Select Path, VHDType, VHDFormat, $VHDsGB
$output = new-object psobject
$output | add-member noteproperty "VM Name" $VM.Name
$output | add-member noteproperty "RAM(GB)" $VMsRAM
$output | add-member noteproperty "vCPU" $VMsCPU
$output | add-member noteproperty "State" $VMsState
$output | add-member noteproperty "Status" $VMsStatus
$output | add-member noteproperty "Uptime" $VMsUptime
$output | add-member noteproperty "Start Action" $VMsAutomaticstartaction
$output | add-member noteproperty "Integration Tools" $VMsIntegrationServicesVersion
$output | add-member noteproperty "Replication State" $VMsReplicationState
$output | add-member noteproperty "VHD Path" $VHDs.Path
$output | add-member noteproperty "Size GB" $VHDs.File_Size
$output | add-member noteproperty "VHD Type" $VHDs.vhdtype
$output | add-member noteproperty "VHD Format" $VHDs.vhdformat
$outputArray += $output
}
接下来,我使用 html 格式抛出输出
#Export contents to htm
if($outputArray -ne $null) {
$HTML5 = '<style type="text/css">
#Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
#Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
#Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#Header tr.alt td {color:#000;background-color:#EAF2D3;}
</Style>'
$HTML5 += "<font face=verdana size=4 color=#23B108><b><CENTER>VMGuest Tech Spec's</CENTER></b></font>"
$HTML5 += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 width=100% id=Header>
<TR>
<TH><B>VM Name</B></TH>
<TH><B>RAM(GB)</B></TD>
<TH><B>vCPU</B></TD>
<TH><B>State</B></TD>
<TH><B>Uptime</B></TD>
<TH><B>Integration Tools</B></TD>
<TH><B>Replication State</B></TD>
<TH><B>VHD Path</B></TD>
<TH><B>Size GB</B></TD>
<TH><B>VHD Type</B></TD>
<TH><B>VHD Format</B></TD>
</TR>"
Foreach($Entry in $outputArray){
$HTML5 += "<TR>
<TD>$($Entry.'VM Name')</TD>
<TD>$($Entry.'RAM(GB)')</TD>
<TD>$($Entry.vCPU)</TD>
<TD>$($Entry.State)</TD>
<TD>$($Entry.Uptime)</TD>
<TD>$($Entry.'Integration Tools')</TD>
<TD>$($Entry.'Replication State')</TD>
<TD>$($Entry.'VHD Path')<BR></TD>
<TD>$($Entry.'Size GB')</TD>
<TD>$($Entry.'VHD Type')</TD>
<TD>$($Entry.'VHD Format')</TD>
</TR>"
}
上面的代码工作正常。但是,“$($Entry.'VHD Path')
”会导致多个路径,因为有多个虚拟磁盘&这是我的报告。
现在,我的问题是如何将每个输出格式化为表格内的单独一行。
提前感谢您的帮助。
【问题讨论】:
-
我是否正确理解
$Entry.'VHD Path'是一个数组? (或者可能只是一个逗号分隔的字符串......?) -
是的,它是数组的一部分...在数组中,我为我获取的每个值提供标签。以下收集所有路径信息和其他信息。 $VHDs = 获取-VHD -ComputerName $VM.ComputerName -VMId $VM.Id |选择 Path, VHDType, VHDFormat, $VHDsGB 现在只有路径元素 $VHDs.Path 被赋予了标签 $output | add-member noteproperty "VHD Path" $VHDs.Path 最后在 HTML 格式输出中使用标签名称
$($Entry.'VHD Path')
标签: html powershell formatting