【问题标题】:Grab all VMs in Hyper-V cluster Windows 2012 R2 and their disks sizes获取 Hyper-V 群集 Windows 2012 R2 中的所有 VM 及其磁盘大小
【发布时间】:2016-03-07 15:04:50
【问题描述】:

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

$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 主机。 如何让集群中的所有虚拟机从一个节点以 HTML 格式输出?

【问题讨论】:

    标签: powershell hyper-v windows2012


    【解决方案1】:

    这样的事情怎么样?

    $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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多