Powershell 枚举属性的属性

标签 powershell powershell-3.0

我有一个从设备寄存器收集信息的命令行开关:

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

HostName: ServerA
OOB:
Criticality: Normal
IsVirtual: True

等等

其中一些在内部有一个“子属性”数组,例如:

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}

我可以使用类似这样的方法在数组中获取我想要的任何属性:

(Get-PSDevice ServerA).cluster.name

我正在尝试确定一种使用 foreach 类型语句来枚举所有子属性的方法来填充一个值。

实现此目标的最佳方法是什么?

最佳答案

PowerShell 中的每个对象都有一个隐藏的 .PSObject 属性,它告诉您有关该对象的信息。 its 属性之一是 .Properties 属性(正如 PetSerAl 指出的那样,它不是属性,实际上是 MemberSet,尽管您可以访问它具有属性语义)。

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

这将返回 [PSProperty] 对象,向您显示有关属性的信息(名称、值、类型、是否可获取和可设置等)。

关于Powershell 枚举属性的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44710664/

相关文章:

powershell - 尝试,catch 似乎不起作用

powershell - 替换包含$个字符的字符串

powershell - 避免这种 $null 测试的方法

powershell - $ null是否应位于等式比较的左侧? (带有数组的-eq)

powershell - 通过PowerShell安装时选择安装目录

windows - 将 Consul 作为 Windows 服务启动

powershell - PowerShell匹配,包含不适用于网络共享路径

powershell - 用于在Active Directory中提取各种数据字段的Powershell脚本

powershell - 如何在PowerShell中按名称和值过滤注册表项下的名称/值对?