simsaull
simsaull

Reputation: 329

Powershell, JSON and NoteProperty

I'm trying to use Zabbix API with powershell to automate some monitoring stuff. I'd like to retrieve "items" based on different parameters passed to my function to do something like this : if -itemDescription parameter is passed, look for this description and/or if parameter -host is passed limit scope to that host etc... You can find the method description here : https://www.zabbix.com/documentation/1.8/api/item/get

This is a correct request :

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

So, I know how to add several "params", I did it for the host.create method, with something like this :

$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} |
...

What I don't know however is how to make it conditional. I can't find the right syntax to add a "if" statement in the middle of that line. Something like :

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

Thanks a lot guys!

Also, pleaser pardon my English, it's not my 1st language

Upvotes: 2

Views: 1645

Answers (1)

simsaull
simsaull

Reputation: 329

Like Kayasax, i created my "params" before passing it to add-member. FYI, this is my woring code :

#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

Upvotes: 1

Related Questions