我正在尝试为每个服务器列表创建一个JSON文件,其中包含每个服务器的特定参数。然后,我想将JSON作为对象导入PowerShell,并能够相应地导航数据。这是我的第一次尝试:
[
{
"Name": "server01",
"Drives": [
{"id": "C:", "threshold": 100},
{"id": "F:", "threshold": 100}
],
"Services": ["service1","service2","service3"]
},
{
"Name": "server02",
"Drives": [
{"id": "C:", "threshold": 100},
{"id": "F:", "threshold": 100},
{"id": "G:", "threshold": 100}
],
"Services": ["service1","service2"]
}
]
PowerShell脚本:
$svr = (Get-Content -Raw -Path c:\scripts\servers.json | ConvertFrom-Json)
$svr
输出
Name Drives Services ---- ------ -------- server01 {@{id=C:; threshold=100}, @{id=F:; threshold=100}} {service1, service2, service3} server02 {@{id=C:; threshold=100}, @{id=F:; threshold=100}, @{id=G:; threshold=100}} {service1, service2}
这似乎到目前为止工作,但是我需要能够使用类似foreach()
循环的方法遍历每一个。做这个的最好方式是什么?我的JSON文件的改进?