Powershell,JSON和NoteProperty

时间:2013-07-17 16:00:59

标签: json powershell

我正在尝试使用Zabbix API和powershell来自动化某些监控内容。 我想基于传递给我的函数的不同参数来检索“items”以执行以下操作:if -itemDescription参数传递,查找此描述和/或参数-host是否将限制范围传递给该主机等。 .. 您可以在此处找到方法说明:https://www.zabbix.com/documentation/1.8/api/item/get

这是一个正确的请求:

{
"jsonrpc":"2.0",
"method":"item.get",
"params":{
    "output":"shorten",
    "search": {"description": "apache"},
    "limit": 10
},
"auth":"6f38cddc44cfbb6c1bd186f9a220b5a0",
"id":2
}

所以,我知道如何添加几个“params”,我是用host.create方法做的,用这样的东西:

$proxy = @{"proxyid" = "$proxyID"}
$templates = @{"templateid" = "$templateID"}
$groups = @{"groupid" = "$hostGroupID"}
...
Add-Member -PassThru NoteProperty params @    {host=“$hostName”;dns="$hostFQDN";groups=$groups;templates=$templates;proxy_hostid=$proxyID} |
...

但我不知道如何让它成为有条件的。我找不到正确的语法在该行的中间添加“if”语句。类似的东西:

Add-Member -PassThru NoteProperty params @{output="extend";if(itemDescription) {search=$desctiption} } )

非常感谢你们!

另外,请原谅我的英语,这不是我的第一语言

1 个答案:

答案 0 :(得分:1)

像Kayasax一样,我在将它传递给add-member之前创建了我的“params”。 仅供参考,这是我令人担忧的代码:

#construct the params
$params=@{}
$search=@{}
#construct the "search" param
if ($itemDescription -ne $null) {

    $search.add("description", $itemDescription)
    $params.add("search",$search)
} 
#contruct the "host" param
if ($hostName -ne $null) {$params.add("host", $hostname) } 
#finish the params
$params.add("output", "extend")
#construct the JSON object  
$objitem = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
Add-Member -PassThru NoteProperty method 'item.get' |
Add-Member -PassThru NoteProperty params $params |
Add-Member -PassThru NoteProperty auth $session.result |
Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json