Skip to content

Instantly share code, notes, and snippets.

@BenNeise
Created October 29, 2013 12:02
Show Gist options
  • Save BenNeise/7213409 to your computer and use it in GitHub Desktop.
Save BenNeise/7213409 to your computer and use it in GitHub Desktop.
One of the earliest scripts I wrote and felt confident enough to publish on my blog at http://ben.neise.co.uk/index.php/2009/03/vm-object-dns-name-mismatches.
# Get all of the VMs as an object
$objVMs = Get-VM
# Loop through all of the VMs
ForEach ($objVM in $objVMs){
# Get the VM Guest object (which contains the DNS information)
$objGuest = Get-VMGuest -VM $objVM
# Set a variable to the VM object name
$objVMName = $objVM.Name
# Set a variable to the DNS name
$objVMFQDN = $objGuest.Hostname
# Sometimes the FQDN is empty or blank, so we screen those out with an "If not equals"
If (($objVMFQDN -ne "") -and ($objVMFQDN -ne $null)){
# The host name is the first part of the FQDN, so we split it, and take the first (0) segment as our host name
$objVMHostName = $objVMFQDN.split('.')[0]
}
Else {
$objVMHostName = $null
}
# Check that the host name is not null, and if the hostname does not match the VM name, echo the results
If (($objVMHostName -ne $null) -and ($objVMName -notlike $objVMHostName)){
# Write the results on one line, the VM object name, then the host name in square brackets. The "`" is an escape character
Write-Host $objVMName `[$objVMHostName`]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment