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_MACHINE\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters].
You can retrieve this information using the following VBScript:
' || Script begins
'
' Setup constant
const HKEY_LOCAL_MACHINE = &H80000002< /FONT >
' Setup registry object (this is a single line)
Set oReg=GetObject("winmgmts:{impersonationLevel= impersonate}!\\.\root\default:StdRegProv")
' Set the key path and values to look at
strKeyPath = "SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"
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