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
Cheers,
Ben
Comments
- Anonymous
January 24, 2005
Very nice. People have been asking for this in the NG for ages.
Scott - Anonymous
January 24, 2005
So when will Microsoft document the way the additions can talk to the host? I am not on about complicated stuff, but simple things like the information above, or the time. Presumably it is just some sequence of illegal instructions or I/O.
My goal is to be able to write additions for some embedded environments as well as Linux. - Anonymous
January 24, 2005
The comment has been removed - Anonymous
November 10, 2014
Thank you for share!