我尝试从check_mk with http-api "action=get_all_hosts"抓取我的所有主机,响应是json格式,如下所示:
"{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}" 现在,我尝试格式化响应,但没有成功。如何将结果格式化为包含所有属性的table?
发布于 2019-06-11 17:14:58
您粘贴的JSON是不正确的。它的开头和结尾都不应该有引号。最后还少了一个}。您可以使用任何在线工具like this进行验证。
一旦你有了正确的JSON,它应该是:
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}一旦将属性从JSON转换过来,您就可以访问它们:
# Convert and save to variable
$convertedJSON = @"
{"result": {"some host name": {"attributes": {"tag_Chassis": "Vm", "tag_ServerFamily": "WindowsServer", "tag_criticality": "prod", "tag_Application": "AllApp", "alias": "some alias", "ipaddress": "172.21.x.x", "tag_networking": "lan"}, "hostname": "some host name", "path": "windows"}}}
"@ | ConvertFrom-Json
# Access attributes
$convertedJSON.result.'some host name'.attributes
# If you don't know the hostname you can find it like this
($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name
# List all attributes from your JSON
$convertedJSON.result.$(($convertedJSON.result |Get-Member | ? MemberType -eq "NoteProperty").Name).attributes
# Output will be like this
tag_Chassis : Vm
tag_ServerFamily : WindowsServer
tag_criticality : prod
tag_Application : AllApp
alias : some alias
ipaddress : 172.21.x.x
tag_networking : lanhttps://stackoverflow.com/questions/56539894
复制相似问题