Skip to content

Instantly share code, notes, and snippets.

@BenjaminArmstrong
Created June 12, 2017 15:10
Show Gist options
  • Save BenjaminArmstrong/d891426a2847f16b4b8a82da0fd55fd9 to your computer and use it in GitHub Desktop.
Save BenjaminArmstrong/d891426a2847f16b4b8a82da0fd55fd9 to your computer and use it in GitHub Desktop.
$HyperVServer = "."
$VMName = "Test VM"
 
# Get the management service
$VMMS = gwmi -namespace root\virtualization\v2 Msvm_VirtualSystemManagementService -computername $HyperVServer
 
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization\v2" -computername $HyperVServer
 
# SettingType = 3 ensures that we do not get snapshots
$SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.VirtualSystemType -eq "Microsoft:Hyper-V:System:Realized"}
 
# Request the virtual machine state (100), current memory (103) and memory availability (112)
$RequestedInformation = 100,103,112
 
# Get the summary information for just the selected virtual machine
$SummaryInformation = $VMMS.GetSummaryInformation($SystemSettingData, $RequestedInformation).SummaryInformation | select -first 1
# Check that the virtual machine is running
if ($SummaryInformation.EnabledState -eq "2")
{ write-host "Memory information for" $VMName
write-host
write-host "Current memory usage:" $SummaryInformation.MemoryUsage "MB"
write-host "Current memory demand:" ($SummaryInformation.MemoryUsage * (1 - ($SummaryInformation.MemoryAvailable / 100)))"MB"
}
else
{ write-host "The requested virtual machine is not currently running" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment