Unable to get output in logs files - Powershell + PowerCLI

I’m trying to connect vcenter URL and get VM details using powerCLI module from powershell.
I can get outputs when executing from command prompt /PS, but unable to get outputs in log file when start the telegraf service., getting the below error,
Please let me know if i made any mistake.

powercli_if.ps1 code:
if(Connect-VIServer -Server ukad0001xx.dev.net -User xxxxx -Password xxxxxx -ErrorAction SilentlyContinue) { write-host “state=1” } else { write-host “state=0” }

My Telegraf Code:

[outputs.file]]
files = [“C:/PROGRA~1/Telegraf/logs/metrics.txt”]
data_format = “influx”

[[inputs.exec]]
commands = [ 'powershell -Command “C:/Users/bala/Desktop/ps1-scripts/powercli_if.ps1” ’ ]
timeout = “10s”
data_format = “logfmt”

Getting output when executing from CMD
C:\Program Files\Telegraf\bin>powershell -Command “C:\Users\bala\Desktop\ps1-
scripts\powercli_if.ps1”
state=1

Getting errors when start the service:
2022-01-06T08:47:05Z E! [inputs.exec] Error in plugin: logfmt syntax error at pos 51 on line 7: unexpected ‘"’

The error message you show is what confuses me a bit:

2022-01-06T08:47:05Z E! [inputs.exec] Error in plugin: logfmt syntax error at pos 51 on line 7: unexpected ‘"’

Your output should only have a single line with a state and a return code. However, the output references line 7. That makes me think that something else is getting printed out when Telegraf attempt to run the command.

I tried to reproduce with a simpler config:

[[inputs.exec]]
  commands = ["bash ./test.sh"]
  timeout = "10s"
  data_format = "logfmt"

[[outputs.file]]
  files = ["log.out"]
  data_format = "influx"

with this script:

#!/bin/bash
echo "state=1"

and got this output:

exec,host=ryzen state=1i 1641480860000000000
exec,host=ryzen state=1i 1641480870000000000
exec,host=ryzen state=1i 1641480880000000000
exec,host=ryzen state=1i 1641480890000000000
exec,host=ryzen state=1i 1641480900000000000
exec,host=ryzen state=1i 1641480910000000000

I think you tried from linux, I’m tested in windows using powershell script,
PS cant transfer exact output to logs, showing ‘"’ error.
could you please check and share the correct syntax for powershell script?. Thanks in advance.

Even i tried the below method instead of .ps1 file, but getting the same error.

[[inputs.exec]]
commands = [ 'powershell -Command "if(Connect-VIServer -Server ukad0001xx.dev.net -User xxxxx -Password xxxxxx -ErrorAction SilentlyContinue) { write-host “state=1” } else { write-host “state=0” }” ’ ]
timeout = “10s”
data_format = “logfmt”

I can get the output when executing in cmd prompt, but cant get output in logs while start telegraf service.

I know nothing about powershell… that said after a bit of searching I came up with this:

$command="dir"

try {
    Invoke-Expression $command | out-null
    write-host "state=0"
} catch {
    write-host "state=1"
}

Update the top command to be your full command that you need. On success it will print state=0 and on error it will print state=1