gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:54:25 24 4
gpt4 key购买 nike

我有一个 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
}

结果

Result

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

关于json - 如何使用变量访问 powershell PSCustomObject 变量(来自 json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60096309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com