简体   繁体   English

Powershell:在HTML格式内使用powershell命令

[英]Powershell: Using powershell commands inside html formatting

I'm doing a daily check of Hyper-V VM's and i get the output as required in HTML format. 我正在对Hyper-V VM进行日常检查,并按要求获取HTML格式的输出。 However, there is always room for improvement in html formatting and i need assistance with the following problem. 但是,在html格式中总有改进的余地,我需要以下问题的帮助。

I have the following Array 我有以下数组

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

Next, I throw the output using html formatting 接下来,我使用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>"

}

The above code works fine. 上面的代码工作正常。 However, "$($Entry.'VHD Path') 但是,“ $($ Entry.'VHD Path')
" results in multiple path's as there are multiple virtual disk's & this clutter;s my report. Now, My question is how do i format each output into a separate line inside the table. ”会导致出现多个路径,因为存在多个虚拟磁盘,这会导致我的报告混乱。现在,我的问题是如何将每个输出格式化为表格内的单独一行。

Thank you for your help in advance. 提前谢谢你的帮助。

You can use $OFS variable ( http://technet.microsoft.com/en-us/library/hh847796.aspx ). 您可以使用$ OFS变量( http://technet.microsoft.com/en-us/library/hh847796.aspx )。 It specifies the character that separates the elements of an array when the array is converted to a string. 它指定当数组转换为字符串时分隔数组元素的字符。

For example : 例如 :

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

This will output array elements separated by NewLine character instead of whitespace. 这将输出由NewLine字符而不是空格分隔的数组元素。

In your case you will need to use $OFS = "<br />" 在您的情况下,您将需要使用$OFS = "<br />"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM