由于我是Powershell的新手,有人可以在循环部分提供支持吗?以下是来自Test.json文件的json格式:
{
"Pre-Production_AFM": {
"allowedapps": ["app1", "app2"]
},
"Production_AFM": {
"allowedapps": ["app1", "app2"]
}
}我正在阅读下面的json文件
$json = (Get-Content "Test.json“-Raw) | ConvertFrom-Json
我需要循环并获得第一个和第二个对象- "Pre-Production_AFM“和"Production_AFM”一个接一个动态地。
现在,我已经编写了如下代码:
foreach($i in $json){
if($i -contains "AFM"){
Write host "execute some code"
}
}我的问题是-- $i会动态地保存对象"Pre-Production_AFM“吗?如果不是,请建议一个接一个地动态获取对象以供进一步执行的方法。
发布于 2021-05-21 16:31:01
# read the json text
$json = @"
{
"Pre-Production_AFM": {
"allowedapps": ["app1", "app2"]
},
"Production_AFM": {
"allowedapps": ["app1", "app2"]
}
}
"@
# convert to a PSCustomObject
$data = $json | ConvertFrom-Json
# just to prove it's a PSCustomObject...
$data.GetType().FullName
# System.Management.Automation.PSCustomObject
# now we can filter the properties by name like this:
$afmProperties = $data.psobject.Properties | where-object { $_.Name -like "*_AFM" };
# and loop through all the "*_AFM" properties
foreach( $afmProperty in $afmProperties )
{
$allowedApps = $afmProperty.Value.allowedApps
# do stuff
}https://stackoverflow.com/questions/67633275
复制相似问题