Condividi tramite


getting the computer name of a virtual machine

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.

  • If you are using VMM, you can use a simple PowerShell command:

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.

  • If you are using Virtual Server 2005 R2 Sp1+ and the COM API, a new interface was added to support this functionality. The new interface is called IVMGuestOS2 (https://msdn2.microsoft.com/en-us/library/bb427400(VS.85).aspx) and it has a ComputerName property. This property will give you the FQDN name of the OS inside the Virtual Machine provided that the latest version of the VM Additions are installed.
  • If you are using Hyper-V and the WMI namespace, this can be achieved in a couple of different ways.

1) If you just want the Operating system name, you can get that via the GuestOperatingSystem property of the Msvm_SummaryInformation class (https://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.

Option Explicit

Dim WMIService
Dim KvpComponents
Dim VMList
Dim VM
Dim item
Dim component

'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

'Get all the MSVM_ComputerSystem object
Set 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 if
Next

' Get the list of KvpComponents
Set 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 If
Next

Comments

  • Anonymous
    January 01, 2003
    the second parameter of the GetSummaryInformation API needs to be an array as per the documentation of http://msdn.microsoft.com/en-us/library/cc160706(VS.85).aspx

  • Anonymous
    January 01, 2003
    The comment has been removed