Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable expansion within a here-string, powershell

Tags:

powershell

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.

like image 410
Iokanaan Iokan Avatar asked Jul 04 '26 16:07

Iokanaan Iokan


1 Answers

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.

like image 133
Martin Brandl Avatar answered Jul 08 '26 20:07

Martin Brandl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!