Powershell WMI scripting help

Hyper-V PowerShell Scripting Issue

I need to Modify my script to compensate for the following : Total Memory of All Hyper-V Hosts (*combined Total*)

I need to Add a calculation for Node Loss to this script:

I am open to suggestions on this, however a base calculation would be *Total Memory Pooled* / 256GB
As the Avg of my Hosts have 256 GB RAM.

#Total Count of Physical CPU Cores
#Total Count of In use CPU Cores
#Total COunt of Available CPU Cores

$cred = Get-Credential
$serverList  =               Get-ADComputer -Filter "Name -like '*SERVER*'" | Select-Object -ExpandProperty Name

#Get Hyper-V Fail-Over Information
$HyperVFailoverInfo = Invoke-Command -ComputerName $serverList -Credential $cred -ScriptBlock {
    $vmhost = Get-CimInstance -computername $serverList -ClassName Win32_OperatingSystem -ErrorAction Stop
    $win32ProcessorList = Get-WmiObject -Class Win32_Processor
    $vMList = Get-VM
    $freePhysicalMemory = [Math]::Round(($vmhost.FreePhysicalMemory / 1MB), 2)
    $totalPhysicalMemory = [Math]::Round(((Get-WMIObject -Class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum).Sum / 1gb), 2)
    $totalInUseMemory = [Math]::Round(($TotalPhysicalMemory - $FreePhysicalMemory), 2)
    $TotalCPUCoresInUseRM = ($vMList | Where-Object {$_.State -eq 'Running'} | Get-VMProcessor | Measure-Object -Property Count -Sum).Sum
    $TotalCPUCoresInUse = ($vMList | Where-Object {$_.State -eq 'Running'} | Get-VMProcessor | Measure-Object -Property Count -Sum).Sum
    $logicalCpuCount = ($win32ProcessorList | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum
    $totalCPUCoresInUse = (Get-VM | Measure-Object -Property Count -Sum).Sum
    $TotalPooledMemory = $null
    $TotalNodeLoss = $null
    #$TotalPooledMemory = () # Total Memory of All Hyper-V Hosts (Combined)
    #$TotalNodeLoss =   Total Pooled Memory / 256GB

'' | Select-Object -Property `
    @{n = 'Computername'; e = {$ENV:ComputerName}},
    @{n = 'Free Physical Memory GB'; e = {$FreePhysicalMemory}},
    @{n = 'In Use Physical Memory GB'; e = {$totalInUseMemory}},
    @{n = 'Total Physical Memory GB'; e = {$TotalPhysicalMemory}},
    @{n = 'Total Node Loss'; e = {$TotalNodeLoss}},
    @{n = 'TotalPooledMemory'; e = {$TotalPooledMemory}},
    @{n = 'Total Processors'; e = {$logicalCpuCount}},
    @{n = 'TotalVirtualCPUsInUse (running Machines)'; e = {$TotalCPUCoresInUseRM}},
    @{n = 'TotalVirtualCPUsInUse (All Allocated including Stopped)'; e = {$TotalCPUCoresInUs}},
    @{n = 'Total Processors Available';	e = {$logicalCpuCount - $totalCPUCoresInUse}}

} |

Select-Object -Property * -ExcludeProperty PSComputerName, RunspaceId |
    Sort-Object -Property ComputerName, VMName

write-host "Exported Hyper-V Failover Information to c:\Temp\"
$HyperVFailoverInfo | Export-Csv -NoTypeInformation -Path $HyperVFailoverFileName
$HyperVFailoverInfo | Out-GridView

Open in new window

LVL 13
Who is Participating?
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
Well, invoke is a job and all, but you'll be increasing total pooled memory at each returned result.

 Why would you want total pooled memory and number of failovers as a separate column since they will be the same data throughout?

The easiest way would be to write those separately.  Otherwise, you'll have to create a new object and list its results along with the parsed results from the summation.

To sum the physical memory to get a max:
$TotalPooledMemory = $($HyperVFailoverInfo | Measure-Object "Total Physical Memory GB" -Sum).Sum
$TotalNodeLoss = [Math]::Round(($TotalPooledMemory / 256), 0)

Open in new window

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
glad to help :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.