J1raya
J1raya

Reputation: 340

Powershell enumerate properties of properties

I have a commandlet that gathers information from a device register:

PS C:\windows\system32> Get-PSDevice serverA

HostName: ServerA
OOB:
Criticality: Normal
IsVirtual: True

etc

Some of these have an array of 'sub properties' inside, for example:

Cluster : @{Url=https://ps-apps.com/DeviceRegister/api/Clusters/62; VCenterUrl=https://ps-apps.com/DeviceRegister/api/VCenters/2; ClusterId=62; VCenterId=2; Name=Vcenter 1 ABC Prod; DataCenterUrl=https://ps-apps.com/DeviceRegister/api/DataCenters/3; DataCenter=; IsValidated=True; IsExceptionCluster=False; SupportsProdWorkloads=False; SupportsNonProdWorkloads=False; SupportsSqlWorkloads=False; ManagedByabc=False}

I can get whatever property within the aray I want using something like:

(Get-PSDevice ServerA).cluster.name

I'm trying to determine a way to enumerate all of the sub properties using a foreach type statement to populate a value.

What would be the best way to achieve this?

Upvotes: 3

Views: 888

Answers (1)

briantist
briantist

Reputation: 47792

Every object in PowerShell has a hidden .PSObject property which tells you things about the object. One of its properties is a .Properties property (as PetSerAl points out, it's not a property but in fact a MemberSet, though you access it with property semantics).

(Get-PSDevice ServerA).cluster.PSObject.Properties

That would return [PSProperty] objects that show you the information about the properties (the name, value, type, whether it's gettable and settable, etc.).

Upvotes: 3

Related Questions