UPDATE: Fixed the script to also recurse through arrays. It only gets the first item in the array. That’s for a good reason. Else you’ll get millions of items!

Wow! This was quite a challenge. But I did it!
Inspired by my scripts Create a Directory Tree with Powershell and Listing AD Group Members Recursively with Powershell, I responded to a queation in the VI Toolkit Community: “Is it possible to recursively get all the properties of a VI Toolkit object, such as returned by Get-VMHost | Get-View?”

Here is my script. It creates a collection of objects with the property names (full names) and their values (Great for Export-Csv).

Usage: $VMView = Get-VM | Get-View
.\ShowMyVMProperties.ps1 ‘$VMView’

Do not forget the single quotes around the $VMView there. You can use any variable name you like as well as any object from the VI Toolkit.

param([string]$VariableName)

# Function that lists the properties
function Show-Properties
{
Param($BaseName)
If ((Invoke-Expression $BaseName) -ne $null)
{
#Write-Host “Expanding $BaseName”
$Children = (Invoke-Expression $BaseName) | Get-Member -MemberType Property
ForEach ($Child in ($Children | Where {$_.Name -ne “Length” -and $_.Name -notmatch “Dynamic[Property|Type]” -and $_.Name -ne “”}))
{
$NextBase = (“{0}.{1}” -f $BaseName, $Child.Name)
$Invocation = (Invoke-Expression $NextBase)
If ($Invocation)
{
If ($Invocation.GetType().BaseType.Name -eq “Array”)
{
#Write-Host (“{0} is an array.” -f $Child.Name)
# Recurse through subdir
$NextBase = $NextBase + ‘[0]’
Show-Properties $NextBase
}
ElseIf ($Child.Definition -like “VMware*”)
{
#Write-Host (“{0} is a VMware Object” -f $Child.Name)
# Recurse through subdir
Show-Properties $NextBase
}
Else
{
#Write-Host (“{0} is an endpoint.” -f $Child.Name)
$myObj = “” | Select Name, Value
$myObj.Name = $NextBase
$myObj.Value = $Invocation
$myObj
}
}
Clear-Variable Invocation -ErrorAction SilentlyContinue
Clear-Variable NextBase -ErrorAction SilentlyContinue
}
}
Else
{
Write-Warning “Expand Failed for $BaseName”
}
}

# Actual start of script
If ((Invoke-Expression $VariableName).GetType().BaseType.Name -eq “Array”)
{
$VariableName = $VariableName + ‘[0]’
}
Show-Properties $VariableName

Tagged with:
 

One Response to List ALL available properties in the VI Toolkit

  1. Harkamal says:

    Wow script. Does a great enumeration job.

    Earlier i used to dump VI database..
    Get-VM | Get-View | FC *

Leave a Reply