VMware Cloud Community
flipster
Contributor
Contributor
Jump to solution

Output only properties of VMHost object

Hi Everyone,

I'm trying to create a text file that contain only the properties of VMHost. For example:

I want

esx-test-01

Not

Name: esx-test-01

How do I go about doing this? It seems so trivial but I'm lost.

Thanks!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
harkamal
Expert
Expert
Jump to solution

Here you go mate, recursively enumerate properties of object. Don't forget to click "Correct Answer" button

#----


Function Get-ALLPropertyNames

{

param($VariableName)

#Function that lists the properties

function Show-Properties

{

Param($BaseName)

If ((Invoke-Expression $BaseName) -ne $null)

{

$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}.” -f $BaseName, $Child.Name)

$Invocation = (Invoke-Expression $NextBase)

If ($Invocation)

{

If ($Invocation.GetType().BaseType.Name -eq “Array”)

{

#Recurse through subdir

$NextBase = $NextBase + ‘[0]‘

Show-Properties $NextBase

}

ElseIf ($Child.Definition -notlike “System*”)

{

#Recurse through subdir

Show-Properties $NextBase

}

Else

{

$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

}

$a = Get-VMHost

Get-ALLPropertyNames ‘$a’

View solution in original post

0 Kudos
3 Replies
harkamal
Expert
Expert
Jump to solution

Get Name Properties:

Get-VMHost | Get-View | % { $_.Name }

Ok, so this means, there is no direct or simple way to enumerate properties for a managed entity as the vmware dataobjects are not pure dotNet types, they are complex data types --meaning nested objects residing on server side. For each managed entiry you must obtain reference to the object and then get a view of it. Get-View will get you the link for the object, its properties, methods etc etc.

Go on read the programming guide for vi sdk -- it took me 2-3 weeks to understand how to work with vi sdk

If you ahve specific requirement, then i can code for you --friday i have some time Smiley Happy

0 Kudos
harkamal
Expert
Expert
Jump to solution

You can use Get-Member to enumerate properties of an object/enumeration..loop through all children unless no more props..

$object | gm -MemberType Property

0 Kudos
harkamal
Expert
Expert
Jump to solution

Here you go mate, recursively enumerate properties of object. Don't forget to click "Correct Answer" button

#----


Function Get-ALLPropertyNames

{

param($VariableName)

#Function that lists the properties

function Show-Properties

{

Param($BaseName)

If ((Invoke-Expression $BaseName) -ne $null)

{

$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}.” -f $BaseName, $Child.Name)

$Invocation = (Invoke-Expression $NextBase)

If ($Invocation)

{

If ($Invocation.GetType().BaseType.Name -eq “Array”)

{

#Recurse through subdir

$NextBase = $NextBase + ‘[0]‘

Show-Properties $NextBase

}

ElseIf ($Child.Definition -notlike “System*”)

{

#Recurse through subdir

Show-Properties $NextBase

}

Else

{

$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

}

$a = Get-VMHost

Get-ALLPropertyNames ‘$a’

0 Kudos