Richard Parmiter

Virtualisation blog and Knowledge Base

  • You are here: 
  • Home
  • Determine vCenter display name from within the VM

Determine vCenter display name from within the VM

Posted on October 17th, 2012

There may be situations where you want to determine the vCenter diaply name for a VM from within the OS of the VM.

This could be useful if cloning multiple virtual desktops from a syspreped template to enable the option to set the machine name to be the same as the vCenter display name. It could also be useful in many other scenarios.

However, by default there is no way to do this using the standard VM tools that are instaled into the virtual machine.

It is possible though to set a custom attribute on the VM object in vCenter and then query this from within the the OS of the virtual machine.

The following script can be run using the vSphere PowerCLI to set a custom attribute to be the same as the vCenter display name:

$vServer= “vCenter.server.fqdn”
$vmName = “VM display name”

If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}

Connect-VIServer $vServer | out-null

$vmSet = GET-VM $vmName | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$gInfo = New-Object VMware.Vim.optionvalue
$ginfo.Key=”guestinfo.hostname”
$gInfo.Value=$vmSet.Name
$vmConfigSpec.extraconfig += $gInfo
$vmSet.ReconfigVM($vmConfigSpec)

Disconnect-VIServer $vServer -Confirm:$false | out-null

Once this is set, it is possible to query it within the VM, using the VM tools and the following command:

vmtoolsd.exe –cmd “info-get guestinfo.hostname”

This can of course, be added to scripts to set this attribute against multiple machines.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • FriendFeed
  • Google Buzz
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • RSS

Tags: , , , ,
Filed under Scripts/Reg, VMWare |

One Response to “Determine vCenter display name from within the VM”

  1. Nav Says:
    April 16th, 2014 at 5:13 pm

    Looks like this only works on ESXi5.0 or later

    http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2073055

Leave a Reply

*