Powershell通过API获取Zabbix主机

时间:2019-01-10 13:24:47

标签: powershell zabbix

我正在尝试使用Powershell从Zabbix API获取主机数据。

我想找回主机组15、24、26的以下列:

  • hostid
  • 主机
  • 状态
  • interfaceid
  • ip
  • dns
  • useip

如果我使用Postman提交查询,我将提交以下可行的方法:

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host",
            "status"
        ],
        "groupids": [15, 24, 26],
        "selectInterfaces": [
            "interfaceid",
            "ip",
            "dns",
            "useip"
        ]
    },
    "id": 2,
    "auth": "xxxxxxxxxxxxx"
}

到目前为止,我具有以下Powershell,它返回了很多信息

$params.body = @{
    "jsonrpc"= "2.0"
    "method"= "host.get"
    "params"= @{
        output = "extend"
        selectHosts = "extend"
    }
    auth= "xxxxxxxxxxxxx"
    id= 2
} | ConvertTo-Json

$result = Invoke-WebRequest @params

Write-Host $result

我在理解如何仅请求所需信息方面遇到困难,我之前没有做过这样的powershell脚本,因此希望获得任何指导。

1 个答案:

答案 0 :(得分:1)

您需要使用邮递员中使用的相同字段和选项来构建$params.body

$params.body = @{
    "jsonrpc"= "2.0"
    "method"= "host.get"
    "params"= @{
        output = @( "host", "hostid", "status" )
        selectInterfaces = @( "interfaceid", "ip", "dns", "useip" )
        groupids = @( "15", "24", "26")
    }
    auth = xxxxxxxxxxxxx
    id = 2
} | ConvertTo-Json

您应该得到类似的东西:

hostid host        status interfaces                                                                                          
------ ----        ------ ----------                                                                                          
10017  somehost    0      {@{interfaceid=30251; ip=192.168.10.15; dns=; useip=1}}                                               
10051  anotherone  0      {@{interfaceid=12353; ip=10.10.10.20; dns=tanotherone.mydomain.com; useip=1}}                       
10054  whatisthis  0      {@{interfaceid=43262; ip=172.16.1.20; dns=; useip=1}}