Monday, March 8, 2010

How to determine a VM's management operating system (Hyper-V)?

I recently came across a VM while generating a server list.  I have a script that queries Active Directory for all server-based operating systems.  I take that script and query the servers for the make/model info.  Recently, I saw a VM in the list and was curious because I wasn't familiar with it.  My script uses WMI to query two items:

SystemManufacturer
SystemProductName (AKA Model)
 
For Hyper-V VM's, the SystemManufacturer is Microsoft Corporation.
For Hyper-V VM's, the SystemProductName is Virtual Machine.
 
There are a number of ways to get the values.  WMI using VBScript.  PowerShell.  Or just reading the values from the registry via REGEDIT.
 
REGEDIT:  HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SystemInformation
 
PowerShell:  Get-WmiObject -Query "select * from Win32_ComputerSystem"
 
VBScript (watch out for line wrapping, tie this VBScript into larger script):
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
objTextFile.WriteLine strComputer
objTextFile.WriteLine objItem.Manufacturer & " " & objItem.Model
objTextFile.WriteLine
Next

If anybody wants the entire script (for automating querying a large number of servers), comment and I'll post it.

However, as others have pointed out (see:  http://www.commandbreak.com/CommentView,guid,53004814-7f71-4772-bba2-4b77988d9b73.aspx#commentstart) - other Microsoft virutalization platforms return that same data.  So you have to look a little further to distinguish Hyper-V VM's:
 
BIOSReleaseDate
 
Generally speaking, the BIOSReleaseDate for Hyper-V VM's will be 2008 and later while everything else will be older (2006, etc.).  This isn't that concrete but it's a starting point.  But now what?  Even if you determine that you have a Hyper-V VM, how do you determine which Hyper-V host it resides on?  Where is the management operating system?
 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters
 
There are three values here that are of interest:
 
HostName REG_SZ

PhysicalHostName REG_SZ
PhysicalHostNameFullyQualified

Here is where I finally found the concrete answer.  Once you have the host name, you can quickly manage it and get the rest of the details you need.  I haven't had a chance to compare this data with that of Virtual Server.  But I think I'll build some of this data into my scripts.

For those asking for the same thing but for VMWare (ESX or VSphere) - I cross checked this against both and while the make/model info is valid, the rest isn't.  I will have to dive into determine your host ESX/VSphere server from a VMWare VM another time.