0

I'm trying to automate some Terraform work on Windows. I want to export my Terraform state as JSON then use the Windows version of jq.exe to pull out relevant bits of information.

Ideally, my command line would look like:

terraform show -json | jq '<my-jq-query>'

Unfortunately, by default Windows appears to use UTF-16 LE (so that's what the Terraform JSON output is encoded with) and jq.exe only supports UTF-8.

I found that the PowerShell command Set-Content has an -Encoding parameter that can be used to specify output encoding, but I can't figure out how to get Set-Content to read from stdin instead of from a file. I mean, I'd like to do:

terraform show -json | Set-Content -Encoding utf8 | jq '<my-jq-query'>

but I can't figure out how to get it to work.

How can I coax PowerShell into allow me to convert character encoding in the pipeline without reading/writing to a file?

Nevo
  • 752
  • 2
  • 9
  • 22
  • 1
    What is your `[System.Console]::OutputEncoding`? Maybe `chcp 65001` in the `cmd` console (before running PowerShell) _and_ `[System.Console]::OutputEncoding=[System.Text.Encoding]::UTF8` (before the `terraform show -json | jq ''`) could help? – JosefZ May 20 '22 at 15:12
  • You might also try to set both in/out encoding: `[console]::InputEncoding = [console]::OutputEncoding = [System.Text.Encoding]::UTF8`. See also: https://stackoverflow.com/a/49481797/7571258 – zett42 May 20 '22 at 15:16

0 Answers0