Telegraf not able to execute one, of many, powershell scripts

I have a Powershell script to collect data from a database server. The script runs fine in the OS, but fails with an exit code 1 then i run a telegraf test (telegraf --config telegraf.d\my_db_conf.conf --test --debug)

The output is:

2020-04-09T22:44:14Z E! [inputs.exec]: Error in plugin: exec: exit status 1 for command ‘powershell C:/Progra~1/telegraf
/telegraf.d/Scripts/script.ps1’: C:/Progra~1/telegraf/telegraf.d/Scripts/script.ps1 : The term …

I’d love to know what comes after “The term …” but I cannot capture that in the log or in the console.

The PowerShell script is:

    $sql = @"
select ...
from sys.stats s
join ... 
where ...
    $dbs = Invoke-SqlCmd -Database master -Query "select name from sys.databases where name != 'tempdb'" -QueryTimeout 60;
    $stats = @();
    $results = @();
    foreach($db in $dbs) {
        $stats = Invoke-Sqlcmd -Database $ -Query $sql -QueryTimeout 60;
        $stats | % {
            $results += @{
                database_name = $_.database_name;
                schema_name = $_.schema_name;
                table_name = $_.table_name;
                stat_name = $_.stat_name;
                stat_leading_column = $_.stat_leading_column;
                last_udpdate_date = $_.last_updated;
                rows = $_.rows;
                unfiltered_rows = $_.unfiltered_rows;
                rows_sampled = $_.rows_sampled;
                modifications = $_.modification_counter;
                update_threshold = $_.update_threshold;
    $json_result = ConvertTo-Json -InputObject $results;
    return $json_result;

Collector config is:

	interval = "6h"
	commands = ["powershell C:/Progra~1/telegraf/telegraf.d/Scripts/script.ps1"]
	timeout = "2m"
	name_override = "sql_statistics"

There are multiple PowerShell scripts that use this same config (different name_override and different commands parameters) that work just fine.