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.

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

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.

PowerShell

1

Get-VIObjectByPath-Path'/Folder11'

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

PowerShell

1

2

Get-VIObjectByPath-NoValue'na'|

Export-Csv-Pathc:\Temp\inventory.csv-NoTypeInformation-UseCulture

The resulting CSV file looks something like this

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.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

functionTest-NodePath{

param(

[String]$Path

)

$nodeObj=Get-VIObjectByPath-Path$Path

if($nodeObj.Found){$true}else{$false}

}

foreach($entryin(Import-CsvC:\Temp\inventory.csv-UseCulture)){

$path=$entry.Path

Write-Host"$($Entry.Type) $($entry.Name)"

if($entry.Path-ne'na'){

Write-Host-ForegroundColorYellow"`tPath "-NoNewline

Write-Host"$($Entry.Path)"-NoNewline

if(Test-NodePath-Path$entry.Path){

Write-Host-ForegroundColorGreen"`tok"

}

else{

Write-Host-ForegroundColorYellow"`tnok"

}

}

if($entry.BluePath-ne'na'){

Write-Host-ForegroundColorCyan"`tBluePath "-NoNewline

Write-Host"$($Entry.BluePath)"-NoNewline

if(Test-NodePath-StartNode$rootFolder-Path$entry.BluePath){

Write-Host-ForegroundColorGreen"`tok"

}

else{

Write-Host-ForegroundColorYellow"`tnok"

}

}

}

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