没有其他标签的Powershell ConvertTo-HTML表

时间:2015-03-20 11:01:21

标签: html powershell

我有一个powershell脚本,它循环通过VMware资源池从每个群集中获取信息:

    Foreach($cluster in $clusters){
        Add-Content $report "<h3>$cluster</h3>"

        $rpools = $null
        [array]$rpools = Get-ResourcePool -Location $cluster
        ### Get total memory in cluster ##
        $clusterhosts = get-vmhost -Location $cluster
        $totalclustermem = ($clusterhosts.memorytotalmb | measure -sum).sum
        $totalclustermem = "{0:N0}" -f $totalclustermem


        $objAverage = $null
        $sharesallocation = $null
        $sharesallocation = @()

        ## Enumerate Members of RPools
        Foreach ($rpool in $rpools){
            If ($rpool.name -ne "Resources"){

                $rpoolvms = $rpool | Get-VM
                if($rpoolvms -ne 0){
                    $totalvms = ($rpool | Get-VM).count
                    $totalram = "{0:N0}" -f ($rpoolvms.MemoryMB | Measure-Object -sum).sum
                    $totalcpu = "{0:N0}" -f ($rpoolvms.NumCPU | Measure-Object -sum).sum


                    ### Calculate current Shares ###

                    [int]$totalmemshares = $rpool.NummemShares
                    [int]$totalcpushares = $rpool.NumCpuShares

                    $totalpercpu = "{0:N2}" -f ($totalcpushares/$totalcpu)
                    $totalpermem = "{0:N2}" -f ($totalmemshares/$totalram)

                    $objAverage = New-Object System.Object
                    $objAverage | Add-Member -type NoteProperty -name ResourcePool -value $rpool.name
                    $objAverage | Add-Member -type NoteProperty -name "RAM Shares" -value $totalmemshares 
                    $objAverage | Add-Member -type NoteProperty -name "CPU Shares" -value $totalcpushares 

                    $objAverage | Add-Member -type NoteProperty -name "Total VMs" -value $totalvms
                    $objAverage | Add-Member -type NoteProperty -name "Total VM RAM (MB)" -value $totalram
                    $objAverage | Add-Member -type NoteProperty -name "Total VM CPU" -value $totalcpu
                    $objAverage | Add-Member -type NoteProperty -name "Shares Per MB RAM" -value $totalpermem
                    $objAverage | Add-Member -type NoteProperty -name "Shares Per CPU" -value $totalpercpu
                    if($reserved){
                        $objAverage | Add-Member -type NoteProperty -name "RAM Reservation" -value $rpool.MemReservationMB
                        $objAverage | Add-Member -type NoteProperty -name "CPU Reservation" -value $rpool.MemReservationMhz
                    }
                    if($limit){
                        $objAverage | Add-Member -type NoteProperty -name "RAM Limit" -value $rpool.MemLimitMB
                        $objAverage | Add-Member -type NoteProperty -name "CPU Limit" -value $rpool.CpuLimitMhz
                    }

                    $sharesallocation  += $objAverage
                }
            } 
        }
        $sharesallocation | ConvertTo-Html -head $HtmlHeader | Add-Content $report

    }

我遇到的问题是使用ConvertTo-Html每次运行时都会添加标签。所有我想要它做的是输出表HTML没有其他HTML,我怎么能实现这一点?

1 个答案:

答案 0 :(得分:3)

如果您只想要表格html,请使用-Fragment开关:

$sharesallocation | ConvertTo-Html -Fragment | Add-Content $report

您需要注意将Header和Body标签添加到$报告周围。