I have a PSCustomObject which has list of sub-objects like this:
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}
etc.
Part of the object as 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 I get to array with
$Keys = $Hash | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
I can get the keys into array and iterate them but I cannot access properties by giving the key with variable:
foreach ($key in $Keys) {
Write-Host "key" $key
$data = $KeyValue.$key
}
Result: key aadAuthenticationInServiceFabricMonitoringEffect
and data empty
However, this works:
$KeyValue.vmssSystemUpdatesMonitoringEffect
And this:
$key= "aadAuthenticationInServiceFabricMonitoringEffect"
$KeyValue.$key
How could I get this working with variable?