Powershell:在html格式中使用powershell命令

时间:2014-01-08 05:07:33

标签: html powershell formatting

我正在每天检查Hyper-V VM,并按照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')”“导致多个路径,因为有多个虚拟磁盘和&amp;这个混乱;我的报告。 现在,我的问题是如何将每个输出格式化为表格内的单独行。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用$ OFS变量(http://technet.microsoft.com/en-us/library/hh847796.aspx)。它指定在将数组转换为字符串时分隔数组元素的字符。

例如:

$OFS = "`n"
$array = Get-ChildItem $env:USERPROFILE
Write-Host "$array"

这将输出由NewLine字符而不是空格分隔的数组元素。

在您的情况下,您需要使用$OFS = "<br />"