Unable to fetch data using external data source


data “external” “extDateTime” {
program = [“Powershell.exe”, “${path.module}/getDateTime.ps1”]

output “value” {
value = “${data.external.extDateTime.result.dateTime}”

Powershell file getDateTime.ps1 code

$DateTime = Get-Date -Format “yyyyMMddHHmmss”
Write-Output “{”“dateTime”": $DateTime}"

Run: terraform plan

Error: Unexpected External Program Results
│ with data.external.extDateTime,
│ on main.tf line 26, in data “external” “extDateTime”:
│ 26: program = [“Powershell.exe”, “${path.module}/getDateTime.ps1”]
│ The data source received unexpected results after executing the program.
│ Program output must be a JSON encoded map of string keys and string values.
Program: C:\WINDOWS\System32\WindowsPowerShell\v1.0\Powershell.exe
│ Result Error: invalid character ‘{’ after top-level value

My understanding is the PS script has to return JSON but I keep getting the Result Error

As per my understanding, it should be enclosed in quotes.

Not required but I tried it and same error. The error states the issue is the first { which seems odd since json is required.
Thanks for your response.

I figured out what the issue was. I’m running terraform commands in PS 7 (pwsh.exe) but my data.external program was calling PS 5 (powershell.exe) . Once I updated to the following: program = [“pwsh.exe”, “${path.module}/getDateTime.ps1”] it worked fine. The error message referring to the { was misleading me.