Scripting dynamic memory, part 3: looking at performance counters

I have wanted to poke around the Hyper-V performance counters with PowerShell for a while now – and the arrival of dynamic memory gives me a good excuse to do so.  Here is a script that will display all the current dynamic memory performance information of the specified virtual machine:

 # Prompt for the Hyper-V Server to use
 $HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
  
 # Get-Counter does not like "." as a computer name - so look up the local computer name for this case
 if ($HyperVServer -eq ".") {$HyperVServer = $env:COMPUTERNAME}
  
 # Prompt for the virtual machine to use
 $VMName = Read-Host "Specify the name of the virtual machine"
  
 # Get all the dynamic memory performance counters for the virtual machine
 $PhysicalMemory = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Physical Memory" -computername $HyperVServer).CounterSamples[0].CookedValue
 $GuestVisiblePhysicalMemory = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Guest Visible Physical Memory" -computername $HyperVServer).CounterSamples[0].CookedValue
 $AddedMemory = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Added Memory" -computername $HyperVServer).CounterSamples[0].CookedValue
 $RemovedMemory = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Removed Memory" -computername $HyperVServer).CounterSamples[0].CookedValue
 $MemoryAddOperations = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Memory Add Operations" -computername $HyperVServer).CounterSamples[0].CookedValue
 $MemoryRemoveOperations = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Memory Remove Operations" -computername $HyperVServer).CounterSamples[0].CookedValue
 $CurrentPressure = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Current Pressure" -computername $HyperVServer).CounterSamples[0].CookedValue
 $MinimumPressure = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Minimum Pressure" -computername $HyperVServer).CounterSamples[0].CookedValue
 $MaximumPressure = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Maximum Pressure" -computername $HyperVServer).CounterSamples[0].CookedValue
 $AveragePressure = (Get-Counter "\Hyper-V Dynamic Memory VM($VMName)\Average Pressure" -computername $HyperVServer).CounterSamples[0].CookedValue
  
 # Display information
 write-host "Dynamic memory information about" $VMname
 write-host
 write-host "Total memory available to the virtual machine:                     " $PhysicalMemory "MB"
 write-host "Physical memory as reported by task manager in the virtual machine:" $GuestVisiblePhysicalMemory "MB"
 write-host
 write-host "Amount of memory added to the virtual machine since the last boot:    " $AddedMemory "MB"
 write-host "Amount of memory removed from the virtual machine since the last boot:" $RemovedMemory "MB"
 write-host "Amount of memory add operations since the last boot:                  " $MemoryAddOperations
 write-host "Amount of memory remove operations since the last boot:               " $MemoryRemoveOperations
 write-host
 write-host "Current memory pressure:" $CurrentPressure"%"
 write-host "Minimum memory pressure:" $MinimumPressure"%"
 write-host "Maximum memory pressure:" $MaximumPressure"%"
 write-host "Average memory pressure:" $AveragePressure"%"

Cheers,

Ben

DMPerfCounters.zip