telegraf icon indicating copy to clipboard operation
telegraf copied to clipboard

Cannot escaping backslash, spaces, special chars in exec input on Windows

Open EdouardNaze opened this issue 9 years ago • 16 comments

I just setup my telegraf.conf who run a powershell script with a path as an argument. Problem is : My argument can be a network path, and it seems like there are no way to use backslash with the exec input. commands = ['powershell ./parse_send_csv.ps1 "\blabla\a"']

Using \\ results to having 2 backslash : Not working Using only one \ results to no backslash : Not working Using single or double quote or both : Not working I even tried to escape with character (escaping character in golang) with no success.

EdouardNaze avatar Jan 05 '17 14:01 EdouardNaze

Can you provide exactly what you tried and the error you received in each case?

sparrc avatar Jan 06 '17 10:01 sparrc

I think i know the problem, i have similar situation, this is my example:

# powershell script with one argument, for runing it you need to do next:
# run script script.ps1 directoryname, for example
# smb_files_count_arg.ps1 //netapp-04/Shared

$directory = $args[0] -replace '/', '\'
$filescount = $(Get-ChildItem -Recurse -Attributes !Directory $directory | Where {$_.DirectoryName -notmatch 'log'} ).Count

# write result as influxdb format
Write-Host "psscript,SMBDirectory=$directory FilesCount=$filescount"

I need sent for psscript smb network path with slashs, but now it is not possible with telegraf i use replace slash to backslash $directory = $args[0] -replace '/', '\' it is working, but this is not a correct way

dmitrysdm avatar Jan 10 '17 07:01 dmitrysdm

what if you don't put quotes around \blabla\a ?

could you try "powershell ./parse_send_csv.ps1 \blabla\a"? (try with \ and \\)

sparrc avatar Jan 10 '17 09:01 sparrc

@dmitrysdm Not bad, i didn't think about replace slash, thanks :) @sparrc I tested all the case, it's just impossible to have only one backslash, quotes or not, double backslash or not, there is just no way to do that. "powershell ./parse_send_csv.ps1 \blabla\a" result to having "blablaa" as parameter in powershell And with double backslash, result : "\\blabla\\a"

EdouardNaze avatar Jan 11 '17 15:01 EdouardNaze

This is how i make it works after too many tries:

[[inputs.exec]]
  commands = ["powershell C:/scripts/test.ps1"]
  timeout = "5s"
  data_format = "json"
  name_suffix = "_mycollector"

I replaced back slash \ with forward slash / in the script path.

Here is my script that return the status of msmq :

$result = 1;
$ErrorActionPreference = "Stop";

Try
{
 Get-MsmqQueue | out-null  ; $result = 1;
}
Catch
{
$result = 0;
}
Finally {
$json = @{ status = $result} | ConvertTo-Json -Compress;
Write-Host $json
}

ysinjab avatar Feb 02 '17 14:02 ysinjab

We should provide a method for specifying a list of args to the script, instead of depending on go-shellquote to parse them.

danielnelson avatar Mar 10 '17 19:03 danielnelson

Hey @danielnelson @sparrc

I'm trying to run the following powershell script:

function Get-ServiceStatus($service) { $status = [int](Get-Service -Name $service -ErrorAction SilentlyContinue).Status #Write-Host "Service: $service status is $status" if ($status -gt 0 -and $status -lt 5) { Write-Host "win_service,host=$($env:ComputerName),service_name=$($service) status=$($status)" } } $serviceList = (Get-Service).Name foreach ($serviceName in $serviceList){ Get-ServiceStatus $serviceName }

I'm using telegraf exec as follows: [[inputs.exec]] commands = ["powershell C:/Program Files/telegraf/servicecheck.ps1"] timeout = "5s" data_format = "influx"

For some reason I receive the following error in telegraf log: Error in plugin [inputs.exec]: exec: exit status 1 for command 'powershell C:/Program Files/telegraf/servicecheck.ps1

Can you please help me to figure out what am I doing wrong ?

dudusakharovich avatar Jun 29 '17 12:06 dudusakharovich

Looks like there is an error in the powershell script, but I don't know powershell so I can't help. Make sure it exits with status code 0.

danielnelson avatar Jun 29 '17 17:06 danielnelson

@danielnelson have the same problem, tray to move the filte "servicecheck.ps1" to a path with out spaces (wihtespace)

[[inputs.exec]] commands = ["powershell C:/temp/servicecheck.ps1"] timeout = "5s" data_format = "influx"

urskog84 avatar Sep 28 '17 15:09 urskog84

Has anyone been able to get this to work with glob? I'm trying to run several files within a folder like so:

[[inputs.exec]] commands = ["C:/telegraf-scripts/*"] timeout = "5s" data_format = "influx"

However it's giving me the following error, even after trying multiple \\,\,// and / combinations with both the directory path as well as the asterisk itself:

ERROR in input [inputs.exec]: Errors encountered: [exec: exec: "C:telegraf-scriptsservices.ps1": file does not exist for command 'C:\telegraf-scripts\services.ps1'], [exec: exec: "C:telegraf-scriptsAllServices.exe": file does not exist for command 'C:\telegraf-scripts\AllServices.exe'], [exec: exec: "C:telegraf-scriptsServiceStatus.exe": file does not exist for command 'C:\telegraf-scripts\ServiceStatus.exe']

FWIW - Running the scripts individually like this seems to work just fine:

[[inputs.exec]] commands = ["C:/telegraf-scripts/ServiceStatus.exe","C:/telegraf-scripts/AllServices.exe"] timeout = "5s" data_format = "influx"

Is it related to the other cases of Windows and multiple instances not working?

Any ideas would be appreciated.

amitkumar912 avatar Oct 02 '17 19:10 amitkumar912

@amitkumar912 I think this issue will need to be fixed before you will be able to get the globs to work in Windows. It isn't related to those other issues.

I recommend listing the scripts one at a time, as I'd be somewhat uneasy with the idea of executing based on a glob personally.

danielnelson avatar Oct 02 '17 23:10 danielnelson

try this : you can call .ps from .bat

[[inputs.exec]] commands = ["D:/\Script/\ServiceCheck.bat"] timeout = "30s" data_format = "json" name_suffix = "_CanService"

maddygoes avatar May 29 '18 15:05 maddygoes

For anyone still having issues, there is some additional advice posted on #6339.

danielnelson avatar Sep 03 '19 18:09 danielnelson

Any progress? It's been three years, so I was just hoping that ...

mikeblas avatar Feb 22 '21 22:02 mikeblas

i got this working. used \ to escape the double quote within the array and used the & operator within the powershell.exe parameter which allowed the spaces to exist.

[[inputs.exec]]
  commands = ["powershell \"&'C:/path/with some spaces/script.ps1'\""]
  interval = "60s"
  data_format = "influx"

jstiops avatar Mar 21 '21 15:03 jstiops

As mentioned above there is a way to make this work. Closing the issue.

srebhan avatar May 08 '23 16:05 srebhan