如何使用变量访问 powershell PSCustomObject 变量(来自 json)

How to access powershell PSCustomObject variable with variable (from json)

我有一个 PSCustomObject,它有这样的子对象列表:

vmssSystemUpdatesMonitoringEffect                                   : @{type=String; metadata=; allowedValues=System.Object[]; defaultValue=AuditIfNotExists}
vmssEndpointProtectionMonitoringEffect                              : @{type=String; metadata=; allowedValues=System.Object[]; defaultValue=AuditIfNotExists}
vmssOsVulnerabilitiesMonitoringEffect                               : @{type=String; metadata=; allowedValues=System.Object[]; defaultValue=AuditIfNotExists}
systemUpdatesMonitoringEffect                                       : @{type=String; metadata=; allowedValues=System.Object[]; defaultValue=AuditIfNotExists}
systemConfigurationsMonitoringEffect                                : @{type=String; metadata=; allowedValues=System.Object[]; defaultValue=AuditIfNotExists}

等等

部分对象为JSON:

{
  "vmssSystemUpdatesMonitoringEffect": {
    "type": "String",
    "metadata": {
      "displayName": "System updates on virtual machine scale sets should be installed",
      "description": "Enable or disable virtual machine scale sets reporting of system updates"
    },
    "allowedValues": [
      "AuditIfNotExists",
      "Disabled"
    ],
    "defaultValue": "AuditIfNotExists"
  },
  "vmssEndpointProtectionMonitoringEffect": {
    "type": "String",
    "metadata": {
      "displayName": "Endpoint protection solution should be installed on virtual machine scale sets",
      "description": "Enable or disable virtual machine scale sets endpoint protection monitoring"
    },
    "allowedValues": [
      "AuditIfNotExists",
      "Disabled"
    ],
    "defaultValue": "AuditIfNotExists"
  },
  "vmssOsVulnerabilitiesMonitoringEffect": {
    "type": "String",
    "metadata": {
      "displayName": "Vulnerabilities in security configuration on your virtual machine scale sets should be remediated",
      "description": "Enable or disable virtual machine scale sets OS vulnerabilities monitoring"
    },
    "allowedValues": [
      "AuditIfNotExists",
      "Disabled"
    ],
    "defaultValue": "AuditIfNotExists"
  }
}

我用

排列的键
$Keys = $Hash | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name

我可以将键放入数组并对其进行迭代,但我无法通过为键提供变量来访问属性:

foreach ($key in $Keys) {
        Write-Host "key" $key
        $data = $KeyValue.$key  
}

结果: 关键 aadAuthenticationInServiceFabricMonitoringEffect

数据为空

但是,这有效:

$KeyValue.vmssSystemUpdatesMonitoringEffect

还有这个:

$key= "aadAuthenticationInServiceFabricMonitoringEffect"
$KeyValue.$key

我怎样才能让它与变量一起工作?

要遍历 PSObject 的属性,您需要使用 $YourObject.psobject.Properties.Name

遍历属性

请参阅下面的示例,该示例基于您提供的信息。

$Policyset = Get-AzPolicySetDefinition
$Policyset = Get-AzPolicySetDefinition -Name 1f3afdf9-d0c9-4c3d-847f-89da613e70a8 
$policyHash = $Policyset.Properties.parameters

$DataSet = $policyHash.aadAuthenticationInServiceFabricMonitoringEffect
$Keys = $DataSet.psobject.Properties.name

foreach ($key in $Keys) {
    Write-Host $Key -ForegroundColor Cyan
    Write-Host $DataSet.$key
}

结果

补充说明 由于您添加了要迭代嵌套属性,请查看此处提供的答案。 。请注意由于引用父对象而导致的无限循环,因为这确实适用于您的情况。

假设您有一个类似于此的对象:

$KeyValue = @{
vmssSystemUpdatesMonitoringEffect = @{
    type='String'; 
    metadata=''; 
    allowedValues=@(1,2,3); 
    defaultValue='AuditIfNotExists'}
}

我们基本上有一个键值对,其中顶层只包含一个键 vmssSystemUpdatesMonitoringEffect,其值是它自己的嵌套哈希表。

我们可以通过首先在哈希表中查找和 .Keys,然后查找其中的 foreach,在那里查找任何 .Keys 并获取它们的值,来非常轻松地解析它。

$KeyValue = @{vmssSystemUpdatesMonitoringEffect = @{type='String'; metadata=''; allowedValues=@(1,2,3); defaultValue='AuditIfNotExists'}}
foreach($key in $KeyValue.Keys){
    $nestedKeys = $KeyValue.$key.Keys
    "parsing node $key in `$KeyValue` which has $($nestedKeys.Count) nested keys"

    foreach($nestedkey in $nestedKeys){
        "--parsing nested key $nestedKey"
        "--$($KeyValue.$key.$nestedKey)"
        }
}

这会给我们一个输出:

parsing node vmssSystemUpdatesMonitoringEffect in $KeyValue which has 4 nested keys
--parsing nested key defaultValue
--AuditIfNotExists
--parsing nested key allowedValues
--1 2 3
--parsing nested key type
--String
--parsing nested key metadata
--

这应该可以让您开始您感兴趣的道路。

如果您有一个包含哈希表的 PSCustomObject

首先,我非常非常抱歉让您陷入这种痛苦。

其次,如果您处于这种情况,则必须使用两种技术来枚举您邪恶的混蛋对象的节点。

$KeyValue = [pscustomobject]@{vmssSystemUpdatesMonitoringEffect = @{type='String'; metadata=''; allowedValues=@(1,2,3); defaultValue='AuditIfNotExists'}}
$keys = get-member -InputObject $keyvalue -MemberType NoteProperty 
foreach($key in $keys){
    $nestedKeys = $KeyValue.$($key.Name).Keys
    "parsing node $($key.Name) in `$KeyValue` which has $($nestedKeys.Count) nested keys"

    foreach($nestedkey in $nestedKeys){
        "--parsing nested key $nestedKey"
        "--$($KeyValue.$($key.Name).$nestedKey)"
        }
}

最大的区别是我们必须使用 Get-Member cmdlet 检索所有键,并指定我们要检索具有 NoteProperty 类型的成员。这为我们提供了 CustomObject 的所有属性,然后我们将通过这些属性查找具有属性的哈希表。

下一组怪事来自这一行 $nestedKeys = $KeyValue.$($key.Name).Keys,它使用 PowerShell 的子表达式运算符 运行 运行 $( ) 符号中的项目并将输出视为细绳。它类似于 运行ning $KeyValue.vmssSystemUpdatesMonitoringEffect.Keys.

除此之外,语法基本相同。

在你的例子中,不是吗:

$hash = get-content file.json | convertfrom-json
$Keys = $Hash | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
$data = foreach ($key in $Keys) {
  $hash.$key  
}
$data


type   metadata                     allowedValues
----   --------                     -------------
String @{displayName=Endpoint prote {AuditIfNotExists, Disabled}
String @{displayName=Vulnerabilitie {AuditIfNotExists, Disabled}
String @{displayName=System updates {AuditIfNotExists, Disabled}

对我来说,如果对象很难处理,那么设计就很糟糕。我更喜欢这种方式,作为 3 个相似对象的数组:

[
    {
        "header":  "vmssSystemUpdatesMonitoringEffect",
        "type":  "String",
        "metadata":  {
                         "displayName":  "System updates on virtual machine scale sets should be installed",
                         "description":  "Enable or disable virtual machine scale sets reporting of system updates"
                     },
        "allowedValues":  [
                              "AuditIfNotExists",
                              "Disabled"
                          ],
        "defaultValue":  "AuditIfNotExists"
    },
    {
        "header":  "vmssEndpointProtectionMonitoringEffect",
        "type":  "String",
        "metadata":  {
                         "displayName":  "Endpoint protection solution should be installed on virtual machine scale sets",
                         "description":  "Enable or disable virtual machine scale sets endpoint protection monitoring"
                     },
        "allowedValues":  [
                              "AuditIfNotExists",
                              "Disabled"
                          ],
        "defaultValue":  "AuditIfNotExists"
    },
    {
        "header":  "vmssOsVulnerabilitiesMonitoringEffect",
        "type":  "String",
        "metadata":  {
                         "displayName":  "Vulnerabilities in security configuration on your virtual machine scale sets should be remediated",
                         "description":  "Enable or disable virtual machine scale sets OS vulnerabilities monitoring"
                     },
        "allowedValues":  [
                              "AuditIfNotExists",
                              "Disabled"
                          ],
        "defaultValue":  "AuditIfNotExists"
    }
]

然后:

cat file.json | convertfrom-json

header        : vmssSystemUpdatesMonitoringEffect
type          : String
metadata      : @{displayName=System updates on virtual machine scale sets should be installed; description=Enable or disable virtual machine scale sets reporting of system updates}
allowedValues : {AuditIfNotExists, Disabled}
defaultValue  : AuditIfNotExists

header        : vmssEndpointProtectionMonitoringEffect
type          : String
metadata      : @{displayName=Endpoint protection solution should be installed on virtual machine scale sets; description=Enable or disable virtual machine scale sets endpoint
                protection monitoring}
allowedValues : {AuditIfNotExists, Disabled}
defaultValue  : AuditIfNotExists

header        : vmssOsVulnerabilitiesMonitoringEffect
type          : String
metadata      : @{displayName=Vulnerabilities in security configuration on your virtual machine scale sets should be remediated; description=Enable or disable virtual machine scale
                sets OS vulnerabilities monitoring}
allowedValues : {AuditIfNotExists, Disabled}
defaultValue  : AuditIfNotExists