vSphere Object by Path

In an older post, named Folder by Path, I provided a function to retrieve a Folder object by it’s path.

With the recent publication of my Get-InventoryPlus function, I can now get the path to all vSphere objects. So the obvious next step was to create a function, that would be able to use that information and retrieve any vSphere object by it’s path.

IMG_20160602_084338

The function was first demonstrated during the 24th VMUGBe in Mechelen.

The Script

Annotations

Line 26-28: The default StartNode is the RootFolder of the default vSphere server ($Global:DefaultVIServer)

Line 32-87: A inline Helper function that will return all children of a node. When a of the childnode is one of the hidden folders, the helper calls itself recursively. This to avoid returning one of the hidden folders.

Line 38: Depending on the Type of the node, different properties need to be used to find the children of a node.

Line 46-48: A Datacenter node points to four hidden folders. Each of these hidden folders in turn points to a number of other objects of a specific type.

Line 93: To avoid that the Split method returns an empty object when the Path is just the root (/), the [StringSplitOptions]::RemoveEmptyEntries option needs to be used.

Line 105-109: The result is returned as a custom object. The properties of this custom object allows easy access to the results: was the node found, what was the path and what is the actual VIObject.

Sample Usage

Simple

In it’s simplest form, you just provide a path with the Path parameter.

path-1

The result is an object with a number of properties:

  • Path: the path that was passed into the function
  • Found: a Boolean, indicating if the node was found
  • Node: the actual vSphere object

Inventory Check

We can use the output of the Get-InventoryPlus function, record the result, and then check if all the vSphere objects that were reported are still there. Kind of an iCheck 😆

The inventory was created with the following lines

The resulting CSV file looks something like this

path-2

The following script will read the inventory, and use the Get-VIObjectByPath function to check if each of the nodes in the inventory is still there.

The result is displayed on the PS console (yes, I know this must have killed a lot of puppies), and looks like this.

path-3

Looks like the junior admin removed a VM !

 

Enjoy!

 

2 Comments

    Herschelle42

    Minor code correction : The second example in the function help is calling get-inventoryplus instead of get-viobjectbypath

      LucD

      Thanks for spotting that.
      That was the “work” name of the function

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.