Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Find VMware Host and Cluster names for VMs

Posted on February 4, 2020February 27, 2022

In today’s post I am going to share a small script block I used long back. I wanted to get VMware ESXi Host and Cluster name for a Virtual Machine from vCenter. Not necessarily the Host will remain same if I check it again after few hours. However, I just wanted to get that information at a particular time. For fewer VMs you can quickly get it from the vCenter directly. If you have hundreds of VM you will definitely need a script!

First, we will take the list of VMs, vCenter Name and an Output file path as input. You can set those as parameters too.

$iFile = "<Some Path>\serverlist.txt" # Input file containing list of VMs
$oFile = "<Some Path>\VM-Host-Cluster-Details.csv" # Output file to store VM, ESXi Host and Cluster Name
$vCenterName = "myvCenter.<somedomain.com>" # vCenter name to connect

Now, check if provided input file exists at the provided path.If not, exit. Otherwise, connect to the vCenter and loop through each VM Name, check and collect ESXi Host and Cluster name.Finally, write those information into a csv output file. However, before we connect vCenter, we will import the Powershell module for VMware.VimAutomation.Core.

if(!Test-Path $iFile){
	Write-Output "No input file provided. Please check and try again!"
	Exit
}
else{
	if(Test-Path $oFile){
		Remove-Item -Path $oFile -Force
	}
	"VM Name, Host Name, Cluster Name" | Out-File $oFile -Append -Encoding ASCII
	Import-Module VMware.VimAutomation.Core
	Connect-VIServer $vCenterName
	ForEach($srv in Get-Content $iFile){
		$vm = $vHost = $vCluster = ""
		$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = $srv}
		$vHost = & {$vm.UpdateViewData("Runtime.Host.Name"); $vm.Runtime.LinkedView.Host.Name}
		$vCluster = (get-view (get-view -ViewType HostSystem -Filter @{"Name" = $vhost}).parent).name
		"$srv,$vHost,$vCluster" | Out-file $oFile -Append -Encoding ASCII
	}
}
disconnect-viserver $vCenterName

Once the script runs successfully, you will receive an output file with information mentioned above.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
June 2025
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes