Richard Parmiter

Virtualisation blog and Knowledge Base

  • You are here: 
  • Home
  • display name

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
$vmConfigSpec.extraconfig += $gInfo

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.

Tags: , , , ,
Filed under Scripts/Reg, VMWare | 1 Comment »