我有一个 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
我怎样才能让它与变量一起工作?