抓取 Hyper-V 集群 Windows 2012 R2 中的所有虚拟机及其磁盘大小

Grab all VMs in Hyper-V cluster Windows 2012 R2 and their disks sizes

我正在尝试获取 html 报告,其中包含集群中所有 VM 的已配置磁盘大小。我正在尝试列出集群内的所有虚拟机:

$VMs = get-ClusterGroup | ? {$_.GroupType -eq "VirtualMachine" } | Get-VM

这很有魅力。但是,当我尝试循环时:

foreach ($VM in $VMs)
{
 Get-VM -VMName $VM.Name | Select-Object VMId | Get-VHD
}

当我 运行 这样做时,每个不在我当前集群节点上的虚拟机都出现错误。 所以对于每个节点,我 运行 以下命令:

Get-VM -VMName * | Select-Object VMId | Get-VHD | ConvertTo-HTML -Proprerty path,computername,vhdtype,@{label='Size(GB)');expression={$_.filesize/1gb -as [int]}} > report.html

这也很有魅力。但这需要登录到集群中的每个 Hyper-V 主机。 如何使集群中的所有 VM 从一个节点在 HTML 中获得输出?

这样的怎么样?

$nodes = Get-ClusterNode
foreach($node in $nodes)
{
    $VMs=Get-VM -ComputerName $node.name
    foreach($VM in $VMs)
    {
        $VM.VMName
        Get-VHD -ComputerName $node.Name -VMId $VM.VMId | ft vhdtype,path -AutoSize
    }
}

据我所知;对于每个 Get-VHD 调用,您需要节点名称为 -ComputerName-VMId。 出于某种原因,将 Get-VM 传递给 Get-VHD 时未提供节点名称。

您正在寻找的内容,上面没有提供作为要格式化的单个对象的结果(html 或其他)。 然而,有一个内联 ForEach-Object 可以解决问题。

这可能是您要找的:

Get-VM -ComputerName (Get-ClusterNode) | 
ForEach-Object {Get-VHD -ComputerName $_.ComputerName -VMId $_.VMId} | 
ConvertTo-HTML -Property path,computername,vhdtype,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}} > report.html

在一行中:

Get-VM -ComputerName (Get-ClusterNode) | ForEach-Object {Get-VHD -ComputerName $_.ComputerName -VMId $_.VMId} | ConvertTo-HTML -Property path,computername,vhdtype,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}} > report.html

希望这能满足您的需求。享受吧!