Search code examples
powershelloracle-cloud-infrastructure

JQ query in powershell is not working with variable in SELECT


In Power shell i am trying to run ORacle Cloud CLI command and filtering it with JQ. If i use hard coded values in JQ select it is working fine..but if i use a variable instead, i am not getting any data. Please help.

working with hard-coded value in select:

oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(.\"hostname-label\"==\"test1\")'

Failing code witn a variable in Select:

oci compute instance list-vnics --all --compartment-id xxxxx  --profile myprof |C:\myfiles\jq-win64.exe '.data[]|select(.\"hostname-label\"==\"$host_name\")'

Solution

  • PowerShell does not expand variables in single-quoted strings.

    $ociResult = oci compute instance list-vnics --all --compartment-id xxxxx --profile myprof
    $jqExpr = '.data[]|select(.\"hostname-label\"==\"' + $host_name + '\")'
    
    $ociResult | C:\myfiles\jq-win64.exe $jqExpr