3

I have the following variable that contains other variables:

$jsontemplate = @"
{
    "jsonrpc": "2.0",
    "method": "trigger.update",
    "params": {
        "triggerid": "$($zabbixtriggerid)",
        "status": 1
    },
                                               "id": "$($zabbixAuth.id)",
                                               "auth": "$($zabbixAuth.auth)"
                                               }
"@

$($xxx.yyy) format works fine if I run the script manually but I cannot use it because the program that runs the script uses tokens in $() format to insert strings into powershell scripts and will go crazy. Any other way to achieve the same effect? When I use just $xxx.yyy format, .yyy part gets ignored. It gets interpreted only when I use $() format.

1 Answer 1

5

You could use a format string to pass the values:

$jsontemplate = @"
{{
    "jsonrpc": "2.0",
    "method": "trigger.update",
    "params": {{
        "triggerid": "{0}",
        "status": 1
    }},
    "id": "{1}",
    "auth": "{2}"
    }}
"@ -f $zabbixtriggerid, $zabbixAuth.id, $zabbixAuth.auth

Look at the last line where I format the string using the three variables.

Sign up to request clarification or add additional context in comments.

2 Comments

as soon as I add -f $zabbixtriggerid, $zabbixAuth.id, $zabbixAuth.auth part, I get the following error: Error formatting a string: Input string was not in a correct format..
Oh, seems like you have to escape the curly brackets using two of them. I updat may answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.