-1

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\")'
Safney
  • 31
  • 6
  • In short: Only `"..."` strings (double-quoted) perform string interpolation (expansion of variable values) in PowerShell, not `'...'` strings (single-quoted): see [this answer](https://stackoverflow.com/a/40445998/45375) for an overview of PowerShell's _expandable strings_ (interpolating strings) and [this answer](https://stackoverflow.com/a/55614306/45375) for an overview of PowerShell string literals in general. – mklement0 Oct 09 '21 at 18:56

1 Answers1

2

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
Tomalak
  • 332,285
  • 67
  • 532
  • 628