Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Moving on in my “scripting dynamic memory” series, the next thing to look at is how to get the current memory usage and memory available data that we display in the Hyper-V manager. You can get the current memory usage by just looking at MSVM_Memory (https://msdn.microsoft.com/en-us/library/cc136855(v=VS.85).aspx), but unfortunately you cannot get the memory availability that way. To get the memory availability you need to use GetSummaryInformation (https://msdn.microsoft.com/en-us/library/cc160706(v=VS.85).aspx).
GetSummaryInformation is an API that we use as a light weight way to get a bunch of information about a virtual machine. It returns a Msvm_SummaryInformation Class (or a collection of these) (https://msdn.microsoft.com/en-us/library/cc136898(v=VS.85).aspx)
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
# SettingType = 3 ensures that we do not get snapshots
$SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3}
# 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"
# If memory available == 2147483647 then no memory available value has been returned from the virtual machine
if ($SummaryInformation.MemoryAvailable -ne 2147483647)
{ write-host "Current memory availability:" $SummaryInformation.MemoryAvailable"%"}
else
{ write-host "Dynamic memory is not currently active on this virtual machine"}
}
else
{ write-host "The requested virtual machine is not currently running" }
Some things to know:
Cheers,
Ben
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in