VMware Cloud Community
manfred9999
Contributor
Contributor

Use get-inventory to get datastore,cluster,resourcepool,vm's in hirarchy

Hello

I would like to retrieve the Inventory Hirarchy. Like displayed in the Virtual Center.

Datastores-Clusters-Hosts-Resourcepools-VM's

and also the Folderstructure in the Invetory view.

Is this possible with get-inventory?

Thank yo for any help.

Regards

Manfred

0 Kudos
5 Replies
hugopeeters
Hot Shot
Hot Shot

An interesting way to start with this, is to create a PSDrive. You can then walk through the Virtual Infrastructure in the same way you walk through folders using a dosbox (cd and dir):

$root = Get-Folder -NoRecursion
New-PSDrive -Name VI -PSProvider VimInventory -Root '\' -Location $root
cd VI:

0 Kudos
LucD
Leadership
Leadership

There was a similar request in

With a little adaption the script can list all nodes of the tree, not only the VMs.

It lists the Host & Clusters tree and the Virtual Machines & Templates tree.

You could even emulate the yellow and blue folders with a little adaption of the Wrte-Host cmdlets in the function.

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" {
	    Write-Host ($path + "\" + $childfld.Name)
		get-children $childfld $path
	  }
      "VirtualMachine"{
	    $vm = $path + "\" + $childfld.Name
        Write-Host $vm
	  }
	  "Datacenter"{
	    Write-Host ($path + "\" + $childfld.Name)
        get-children $childfld $path
	  }
	  "ClusterComputeResource" {
	    Write-Host ($path + "\" + $childfld.Name)
        foreach($esxMoRef in $childfld.Host){
          $esx = Get-View -Id $esxMorEF
	      $h = $path + "\" + $childfld.Name + "\" + $esx.Name
		  Write-Host $h
		}
	  }
     }
  }
}

# Virtual machines & Templates

Write-Host "Virtual Machines & Templates`n"
Get-Datacenter | %{
  $dc = Get-View -Id ($_).id
  $folder = Get-View -Id $dc.VmFolder
  get-children $folder $dc.Name
}

# Hosts & Clusters

Write-Host "`nHosts & Clusters`n"
Get-Datacenter | %{
  $dc = Get-View -Id ($_).id
  $folder = Get-View -Id $dc.HostFolder
  get-children $folder $dc.Name
}


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

0 Kudos
manfred9999
Contributor
Contributor

Hello

Thank you for the reply. Sorry for answering so late but my last reply was somehow lost....

This is a nice script.

It will take me some time to work through the script. I'm not very good at scripting. Do you know where I can get some more information about the get-view command.?

Regards

Manfred

0 Kudos
LucD
Leadership
Leadership

You can start with the built in help.

Get-Help Get-View -full

In this forum there are some threads that go into the Get-View cmdlet.

For example have a look at .

There are the blog entries from Carter and Hal that go into this cmdlet.

And I probably forget a few. You can probably Google a few more.

And last-but-not-least, Hal's book (Q1 2009 he told us recently) will give you a lot more on this subject Smiley Wink

I almost forgot this hidden treasure!

The VI Object Model webinar from Henry Robinson.

Although nearly 2 years old an excellent introduction to the VI Data Model.

The webinar recording can be found here.

Message was edited by: LucD


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

0 Kudos
oiscoot
Contributor
Contributor

I am trying to get this script to output to a csv, but am having no luck. Any help would be appreciated.

0 Kudos