为什么对于 Powershell Runspace Job 中的 Server 2012R2 托管虚拟机,Get-VHD 文件大小返回 0?

为什么对于 Powershell Runspace Job 中的 Server 2012R2 托管虚拟机,Get-VHD 文件大小返回 0?

我有这个脚本来制作托管在我们 Hyper-V 集群上的虚拟机的 CSV。它运行良好,我甚至让它将每个虚拟机分成一个作业!

附注:我不介意就如何更有效地进行调度提出建议。主要问题是一些集群是 WS 2016,而另一些是 WS 2012R2,它们需要较旧的 Hyper-V PowerShell 模块。我不确定如何并行化不同的模块,所以我只是获取每个 Hyper-V 节点的 VM 列表,并行化它们,然后如果下一个节点需要它,则更改模块。

无论如何,这是我的问题。这是我的脚本。

$numThreads = 4

$ScriptBlock = {
    Param (
        [string]$Cluster,
        [string]$Node,
        $VM
    )

    If ($ADComputer = (Get-ADComputer $VM.Name -Properties OperatingSystem)) {
        $OS = $ADComputer.OperatingSystem
    }
    Else {
        $OS = 'Unknown (not on domain)'
    }

    try {
        Return [pscustomobject] @{
            Cluster               = $Cluster
            Node                  = $Node
            Name                  = $VM.Name
            OS                    = $OS
            State                 = $VM.State
            Status                = $VM.Status
            Uptime                = "{0:dd} days {0:hh} hours" -f $VM.Uptime
            CPUUsage              = $VM.CPUUsage
            ProcessorCount        = $VM.ProcessorCount
            'MemoryDemand (GB)'   = [math]::Round( $VM.MemoryDemand / 1GB, 2 )
            'MemoryAssigned (GB)' = [math]::Round( $VM.MemoryAssigned / 1GB, 2 )
            'VHD Size (GB)'       = [math]::Round( ((Get-VHD -ComputerName $Node -VMId $VM.Id).FileSize | Measure-Object -Sum).Sum / 1GB, 2 )
            Version               = $VM.Version
            'VLAN IDs'            = ($VM | Select-Object -ExpandProperty NetworkAdapters | Select-Object -ExpandProperty VlanSetting).AccessVlanId -Join ", "
            'IP Addresses'        = ($VM | Select-Object -ExpandProperty NetworkAdapters).IPAddresses -Join ", "
        }
    }
    catch {
        Return [pscustomobject] @{
            Cluster = $Cluster
            Node    = $Node
            Name    = $VM.Name
            State   = "Script Fail"
            Status  = $_.Exception.Message
        }
    }
}

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $numThreads)
$RunspacePool.Open()

$Jobs = @()
$VMInfoList = @()

ForEach ($Cluster in 'USHOMECLU0', 'USHOMECLU1') {
    If ((Get-ADComputer $Cluster -Property OperatingSystem).OperatingSystem -Match '2016') {
        Remove-Module Hyper-V
        Import-Module Hyper-V -RequiredVersion 2.0.0.0
    }
    Else {
        Remove-Module Hyper-V
        Import-Module Hyper-V -RequiredVersion 1.1
    }
    ForEach ($Node in Get-ClusterNode -Cluster $Cluster) {
        Get-VM -ComputerName $Node | ForEach-Object {
            $Job = [powershell]::Create().AddScript($ScriptBlock).AddParameter("Cluster", $Cluster).AddParameter("Node", $Node).AddParameter("VM", $_)
            $Job.RunspacePool = $RunspacePool
            $Jobs += New-Object PSObject -Property @{
                Job    = $Job
                Result = $Job.BeginInvoke()
            }
        }
    }
}

# EndInvoke returns the objects from the background threads
ForEach ($Job in $Jobs) {
    $VMInfoList += $Job.Job.EndInvoke($Job.Result)
}

$VMInfoList | Export-Csv -NoTypeInformation C:\PSReports_vms.csv

我遇到的问题是 Windows Server 2012R2 节点上的虚拟机作业始终返回 0:

        'VHD Size'       = ((Get-VHD -ComputerName $Node -VMId $VM.Id).FileSize | Measure-Object -Sum).Sum

但是,对于 Windows Server 2016 节点,所有虚拟机都会返回相应的值。同样有趣的是,如果我捕获分配给这些 Server 2012R2 托管虚拟机的 VHD 大小的精确表达式(用直接字符串替换变量),并在作业的脚本块之外执行它,我就会得到准确的值。

相关内容