A lot of people have asked me how they can get the actual computer name or FQDN of a virtual machine from from the host system. (FYI, this method does not allow you to get the IP address from within the VM - to do that, you would need to use your DNS infrastructure to correlate the MAC address of the VM and get its IP addresses.)
The answer is pretty simple.
get-vmmserver localhost | get-vm "vmname" | select Name, ComputerName, HostName | format-list
To ensure this works, you would need to ensure your VM is running and that you already installed the VM Additions inside the VM (if this is Virtual Server R2 Sp1), or have installed the Integration Services if this is Hyper-V. VMM 2008 will make it easy to install both via a simple action in our Administrator Console.
1) If you just want the Operating system name, you can get that via the GuestOperatingSystem property of the Msvm_SummaryInformation class (http://msdn2.microsoft.com/en-us/library/cc136898(VS.85).aspx)
2) If you want the OS name and the computer name (FQDN), you can use the GuestIntrinsicExchangeItems property of the Msvm_KvpExchangeComponent class to get this. GuestIntrinsicExchangeItems returns an array of embedded Msvm_KvpExchangeDataItem instances which contain the set of key-value pairs that the guest operating system has pushed up to be available for access by external clients. Again the Intergration Components need to be up to date within the Virtual Machine.
Below is a sample script to illustrate how to do this.
Dim WMIServiceDim KvpComponents Dim VMListDim VM Dim itemDim component
'Get instance of 'virtualization' WMI service on the local computerSet WMIService = GetObject("winmgmts:\\.\root\virtualization")
'Get all the MSVM_ComputerSystem objectSet VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem") For Each VM In VMList if VM.Caption = "Virtual Machine" then WScript.Echo "========================================" WScript.Echo "VM Name: " & VM.ElementName WScript.Echo "VM GUID: " & VM.Name WScript.Echo "VM State: " & VM.EnabledState end ifNext
' Get the list of KvpComponentsSet KvpComponents = WMIService.ExecQuery("SELECT * FROM Msvm_KvpExchangeComponent") '.ItemIndex(0)
For Each component in KvpComponents
' ensure that we are displaying the correct settings for the VM based on its instance ID/Name (you would need to replace the ID with the right one below) If component.SystemName = "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" then
Dim GuestItems GuestItems = component.GuestIntrinsicExchangeItems ' Now enumerate the Msvm_KvpExchangeDataItem's that are in XML format For Each item In GuestItems WScript.Echo "========================================" ' if you wanted the FullyQualifiedDomainName or OSName for the VM, you need to parse the XML ' returned and you can get that value Wscript.Echo item Next End IfNext