VMware Cloud Community
Scissor
Virtuoso
Virtuoso

How to capture the "DNS Name" for a Guest?

In the VI Client, if I select Hosts & Clusters -> click on Virtual Machines tab, I get a list of all of my Virtual Machines with lots of different columns of information. Is there a way to get this information saved to a text file? Specifically I want to get a list of the "Name" and "DNS Name" columns .

PowerCli's Get-VM doesn't appear to display the "DNS Name" field. Is there another command I can run to gather this information?

0 Kudos
3 Replies
Scissor
Virtuoso
Virtuoso

Got it! It is under the $vm.Guest.HostName property.

dbis
Enthusiast
Enthusiast

You solved your own problem, but wanted to share the code that I am using to report on systems whose DNS name does not match the VM name.

  1. Get a listing of all VM's that are powered on and have the VMWare tools running

$VMs = Get-VM | where {$_.PowerState -eq "PoweredOn"} | Get-VMGuest | where {$_.State -eq "Running"}

#Define an object to hold the final report.

$report = @()

  1. Loop through all VM's and obtain VM Name and HostName. Remove any domain name from the hostname and turn both strings to Lower Case.

  2. Compare both strings and write them to the report object.

foreach ($VM in $VMs){

$VMname = $VM.VmName.ToLower()

$VMHostName = $VM.HostName.split('.')[0].ToLower()

If ($VMname -ne $VMHostName) {

$row = "" | Select VMName, VMHostName

$row.VMName = $VMname.ToUpper()

$row.VMHostName = $VMHostName.ToUpper()

$report += $row

}

}

#Display the following text before listing the VM's with mis-matching names

clear

Write-Host "The following VM's have a VM Name and a system hostname that are not identical:" -foregroundcolor Yellow

  1. Sort and display the report object.

$report | sort -property VMName

Scissor
Virtuoso
Virtuoso

Thanks for the example code! Here is the one liner I hacked together yesterday afternoon:

Get-VM | Select Name, 
   @{N="DNS Name";E={$_.Guest.HostName}},
   @{N="IP Address";E={$_.Guest.IPAddress}},
   Host, Notes | ConvertTo-csv

0 Kudos