我想将Get-VM
和Measure-VM
cmdlet的输出合并到带有Get-VM
和Measure-VM
的某些列的单个HTML表中。
我想要VMName,State,RAM,DISK列。
Get-VM
给我VMName和State,而Measure-VM
给我RAM和DISk。
因此,我想将这四列合并为一个HTML表。
我创建了如下代码。
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
那我该如何结合呢?
$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
实际结果是我得到了Get-VM
和Measure-VM
的两个不同的表。
预期结果只有一张表,其中包含VM名称,状态,以GB为单位的RAM,以GB为单位的磁盘。
答案 0 :(得分:1)
在构建HTML表之前,在 前合并数据。
$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