Programmatically detecting host information from inside the guest under Virtual PC and Virtual Server

At various times it is convenient to be able to identify the host computer from inside of a virtual machine in a programatic fashion (e.g. When you are deploying a virtual machine or running an automated process).  To help in these scenarios both Virtual Server and Virtual PC 2004 SP1 will populate the virtual machines registry with information on the name of the host computer, and the name assigned to the virtual machine under Virtual PC or Virtual Server.

This information is only available on virtual machines running Windows 95 through Windows Server 2003 with Virtual Machine Additions installed – and is stored in [HKEY_LOCAL_MACHINESOFTWAREMicrosoftVirtual MachineGuestParameters].

You can retrieve this information using the following VBScript:

‘ || Script begins

‘ Setup constant

const HKEY_LOCAL_MACHINE = &H80000002

‘ Setup registry object (this is a single line)
Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\.rootdefault:StdRegProv”)

‘ Set the key path and values to look at
strKeyPath = “SOFTWAREMicrosoftVirtual MachineGuestParameters”
strValueName1 = “HostName”
strValueName2 = “VirtualMachineName”

‘ Get the values from the registry
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName1, dwValue1
oReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName2, dwValue2

‘ Display the results
WScript.Echo “The virtual machines hosts name is: ” & dwValue1
WScript.Echo “The virtual machines name is: ” & dwValue2

‘ || Script ends