1

I'm writing a script on HYPER-V host for getting VMs guest informations. Is there a way to get VMs Operating System name from Hyper-V using powershell?

There are several examples using (Get-WmiObject Win32_OperatingSystem -ComputerName $vmName).name but i should get this information directly from Hyper-V because of domain restrictions.

Also i'm using hyper-v module of powershell but i couldn't see any cmdlets related to OS.

cansin aldanmaz
  • 119
  • 1
  • 5
  • 10
  • Have you looked at [Msvm_VirtualSystemManagementService class](https://msdn.microsoft.com/en-us/library/hh850062(v=vs.85).aspx) already? – vonPryz Jun 29 '16 at 10:33

3 Answers3

3

This could be retrieved from guest intrinsic exchange items.

# Filter for parsing XML data
filter Import-CimXml 
{ 
   # Create new XML object from input
   $CimXml = [Xml]$_ 
   $CimObj = New-Object -TypeName System.Object 

   # Iterate over the data and pull out just the value name and data for each entry
   foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']")) 
      { 
         $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE 
      } 

   foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']")) 
      { 
         $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE 
      } 

   # Display output
   $CimObj 
} 

# 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 virtual machine object
$query = "Select * From Msvm_ComputerSystem Where ElementName='" + $VMName + "'"
$Vm = gwmi -namespace root\virtualization\v2 -query $query -computername $HyperVServer

# Get the KVP Object
$query = "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
$Kvp = gwmi -namespace root\virtualization\v2 -query $query -computername $HyperVServer

Write-Host
Write-Host "Guest KVP information for" $VMName

# Filter the results
try {
    $Kvp.GuestIntrinsicExchangeItems | Import-CimXml | where Name -eq "OSName"
}
catch {
    Write-Host "Not found"
}

From Ben Armstrong’s Virtualization Blog.

t1meless
  • 494
  • 2
  • 9
  • 1
    This is really good answer. But It won't work if VM is down, as matter of fact none of the solutions will work if machine is down. +1 – Amit Shakya Jun 30 '16 at 14:28
1

Unless you're using SCVMM, Guest OS details are not available via Hyper-V PowerShell cmdlets.

You have to query the Guest itself like you've already found.

henrycarteruk
  • 11,908
  • 2
  • 32
  • 38
0

I couldn't run t1meless' script on PowerShell version 7.1, because gwmi is deprecated in 7.1, sources MS Docs and GitHub. I rewrote the script for PS 7.1.

# Script for retrieving Hyper-V Guest Operating System.
# Tested on PowerShell 7.1

# Prompt for the Hyper-V Server to use
$hyper_v_server = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"

# Prompt for the virtual machine to use
$vm_name = Read-Host "Specify the name of the virtual machine"

# Check if VM exists and is running. This script doesn't work if the VM is stopped.
# Capture error output, source: https://stackoverflow.com/a/66861283/3498768
$vm_not_found = $($vm_state = (Get-VM $vm_name).state) 2>&1

if ($vm_not_found -ne $null) {
    Write-Host "$vm_name VM was not found."
    exit
}

if ($vm_state -eq "Off") {
    Write-Host "Cannot retrieve information of $vm_name. The VM is stopped. Only running VM information can be retrieved."
    exit
}

# Get the virtual machine object
$query = "Select * From Msvm_ComputerSystem Where ElementName='" + $vm_name + "'"
$vm = Get-CimInstance -namespace root\virtualization\v2 -query $query -computername $hyper_v_server

# Get associated information
$vm_info = Get-CimAssociatedInstance -InputObject $vm

Write-Host "Guest information for" $vm_name

# Select only required information
$vm_info | Where GuestOperatingSystem |
    Select -Property GuestOperatingSystem |
    Format-List *
Eastman
  • 53
  • 6