Skip to content

Instantly share code, notes, and snippets.

@BenjaminArmstrong
Created September 6, 2016 18:56
Show Gist options
  • Save BenjaminArmstrong/0f024bba223af1d21a0af75ee580b9b7 to your computer and use it in GitHub Desktop.
Save BenjaminArmstrong/0f024bba223af1d21a0af75ee580b9b7 to your computer and use it in GitHub Desktop.
VM Guest OS Inventory - v2
# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
# Get all virtual machine objects on the server in question
$VMs = Get-CimInstance -namespace root\virtualization\v2 Msvm_ComputerSystem -computername $HyperVServer -filter "Caption = 'Virtual Machine'"
# Create an empty hashtable
$table = @{}
# Go over each of the virtual machines
foreach ($VM in [array] $VMs)
{
# Get the KVP Object
$Kvp = Get-CimAssociatedInstance -InputObject $vm -Association Msvm_SystemDevice -ResultClassName Msvm_KvpExchangeComponent
# Get the OSName value out of the guest KVP data
$xml = [xml]($Kvp.GuestIntrinsicExchangeItems | ? {$_ -match "OSName"})
$entry = $xml.Instance.Property | ?{$_.Name -eq "Data"}
# Filter out offline virtual machines and virtual machines which did not return KVP data
if ($entry.Value)
{$value = $entry.Value}
elseif ($VM.EnabledState -ne 2)
{$value = "Offline"}
else {$value = "Unknown"}
# Count up the values and store it in a hashtable
if ($table.ContainsKey($value))
{$table[$value] = $table[$value] + 1 }
else
{$table[$value] = 1}
}
# Display the results in a nicely formated and sorted manner
$table.GetEnumerator() | Sort-Object Name | Format-Table -Autosize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment