0

I want to combine output of the Get-VM and Measure-VM cmdlets into single HTML table with some columns of Get-VM and Measure-VM.

I want the columns VMName, State, RAM, DISK.

Get-VM gives me VMName and State and Measure-VM gives me RAM and DISk. So I want to combine these four columns into a single HTML table.

I have created code as below.

Get-VM –ComputerName $hostnm |
    Select-Object Name, State, Uptime,
        @{Name="RAM In GB";Expression={[Math]::Round($_.MemoryAssigned/1GB, 2)}} |
    ConvertTo-Html -Head $a

Measure-VM –Name * -Computername $hostnm |
    Sort-Object VMName |
    Select-Object VMName,
        @{Name="RAM In GB";Expression={[Math]::Round($_.MaxRAM/1024, 2)}},
        @{Name="Disk Size in GB";Expression={[Math]::Round($_.TotalDisk/1024, 2)}} |
    ConvertTo-Html -Head $a

So how do I combine this?

$vmhost = Get-Content C:\Users\Share1\Desktop\Scripts\VMHosts.txt
$file = "C:\Users\Share1\Desktop\Scripts\output.htm"

$report = "<html> <body>"
$a = "<style>"
$a = $a + "body {font-family: Tahoma; background-color:#fff;}"
$a = $a + "table {font-family: Tahoma;width: $($rptWidth)px;font-size: 12px;border-collapse:collapse;}"
$a = $a + "th {background-color: #cccc99;border: 1px solid #a7a9ac;border-bottom: none;}"
$a = $a + "td {background-color: #ffffff;border: 1px solid #a7a9ac;padding: 2px 3px 2px 3px;vertical-align: middle;text-align:center;}"
$a = $a + "</style>"

$op1 = @()
$op2 = @()
$myresult = @()

$output = foreach ($hostnm in $vmhost) {
    Enable-VMResourceMetering -Name $hostnm -ResourcePoolType @("Processor","VHD","Ethernet","Memory")

    echo "--------------------</br>"
    echo "<b>" $hostnm  "</b></br>"
    echo "--------------------</br>" 

    $op1 = Get-VM –ComputerName $hostnm |
           Select-Object Name, State, Uptime,
               @{Name="RAM In GB"; Expression={[Math]::Round($_.MemoryAssigned/1GB, 2)}} |
           ConvertTo-Html -Head $a 

    Get-VM –Name * -ComputerName $hostnm | Enable-VMResourceMetering
    $op2 = Measure-VM –Name * -ComputerName $hostnm |
           Sort-Object VMName |
           Select-Object VMName,
               @{Name="RAM In GB";Expression={[Math]::Round($_.MaxRAM/1024, 2)}},
               @{Name="Disk Size in GB";Expression={[Math]::Round($_.TotalDisk/1024, 2)}} |
           ConvertTo-Html -Head $a

    $myresult = $op1 + $op2
}

$report = $report + $output + $myresult + "</body></html>"
$report | Out-File $file

Actual result is I am getting two different tables of Get-VM and Measure-VM.

Expected result is only one table which has VM Name, State, RAM in GB, Disk in GB.

1 Answer 1

1

Combine the data before building the HTML table.

$data = foreach ($hostnm in $vmhost) {
    $resources = @{}
    Measure-VM –Name * -ComputerName $hostnm | ForEach-Object {
        $resources[$_.VMName] = $_ | Select-Object MaxRAM, TotalDisk
    }

    Get-VM –ComputerName $hostnm |
        Select-Object Name, State,
            @{n='RAM in GB';e={[Math]::Round($resources[$_.Name].RAM/1024, 2)}},
            @{n='Disk in GB';e={[Math]::Round($resources[$_.Name].TotalDisk/1024, 2)}}
}
$table = $data | ConvertTo-Html -Head $a 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.