我想将Get-VM
和Measure-VM
cmdlet的输出组合成单个HTML表格,其中包含一些Get-VM
和Measure-VM
列。
我想要列VMName,状态,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为GB,磁盘为GB。
在构建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