VMware Cloud Community
juststormY
Contributor
Contributor

Getting Path of ESX like in VCI

Hello all together,

i've got a short question about scripting in powershell.

i would like to have an .csv report which shows me the "path" to all ESX hosts like it is in VCI.

For example the following:

Folder (Host&Clusters) - Folder (Productive) - Datacenter (Location 1) - Folder (Testcenter) - Cluster (Cluster1) - ESXHost1

Is there are quick way to do this or have anybody done this before?

Thanks a lot for your support.

Best regards.

0 Kudos
1 Reply
LucD
Leadership
Leadership

Does this produce what you wanted to see in the CSV ?

It's based on the script in .

function get-children($entity,$path){
  if($entity.Name -ne "vm"){
    $path += ("\" + $entity.Name)
  }
  foreach($child in $entity.ChildEntity){
    $childfld = Get-View -Id $child
	switch($childfld.gettype().name){
	  "Folder" {
		get-children $childfld $path
	  }
      "VirtualMachine"{
	    $vm = $path + "\" + $childfld.Name
	  }
	  "Datacenter"{
        get-children $childfld $path
	  }
	  "ClusterComputeResource" {
        foreach($esxMoRef in $childfld.Host){
          $esx = Get-View -Id $esxMorEF
	      $h = $path + "\" + $childfld.Name + "\" + $esx.Name
		  $row = "" | Select Host
		  $row.Host = $h
          $script:report += $row
		}
	  }
	  "ComputeResource"{
	      $h = $path + "\" + $childfld.Name
		  $row = "" | Select Host
		  $row.Host = $h
          $script:report += $row
	  }
     }
  }
}

$script:report = @()

# Hosts & Clusters

Get-Datacenter | %{
  $dc = Get-View -Id ($_).id
  $folder = Get-View -Id $dc.HostFolder
  get-children $folder $dc.Name
}
$script:report | Export-Csv "C:\ESX-hosts.csv" -noTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos