Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
EdouardNaze opened this issue Jan 5, 2017 · 16 comments
Closed
Labels
area/exec bug unexpected problem or unintended behavior platform/windows

Comments

@EdouardNaze
Copy link

EdouardNaze commented Jan 5, 2017

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 EdouardNaze changed the title Exec input can't contains only one backslash in the commands array. Exec input can't use only one backslash in the commands array. Jan 5, 2017
@EdouardNaze EdouardNaze changed the title Exec input can't use only one backslash in the commands array. Exec input can't have only one backslash in the commands array. Jan 5, 2017
@EdouardNaze EdouardNaze changed the title Exec input can't have only one backslash in the commands array. Exec input don't work with Windows network path. Jan 5, 2017
@EdouardNaze EdouardNaze changed the title Exec input don't work with Windows network path. Exec input doesn't work with Windows network path. Jan 5, 2017
@EdouardNaze EdouardNaze changed the title Exec input doesn't work with Windows network path. Exec input doesn't work with Windows network path (Can't use only one backslash). Jan 5, 2017
@sparrc
Copy link
Contributor

sparrc commented Jan 6, 2017

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

@dmitrysdm
Copy link

dmitrysdm commented Jan 10, 2017

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

@sparrc
Copy link
Contributor

sparrc commented Jan 10, 2017

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

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

@EdouardNaze
Copy link
Author

EdouardNaze commented Jan 11, 2017

@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"

@ysinjab
Copy link

ysinjab commented Feb 2, 2017

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
}

@danielnelson
Copy link
Contributor

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

@dudusakharovich
Copy link

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 ?

@danielnelson
Copy link
Contributor

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.

@urskog84
Copy link

@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"

@amitkumar912
Copy link

amitkumar912 commented Oct 2, 2017

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.

@danielnelson
Copy link
Contributor

@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 danielnelson added bug unexpected problem or unintended behavior area/exec labels Jan 30, 2018
@maddygoes
Copy link

try this :
you can call .ps from .bat

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

@danielnelson
Copy link
Contributor

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

@danielnelson danielnelson changed the title Exec input doesn't work with Windows network path (Can't use only one backslash). Cannot escaping backslash, spaces, special chars in exec input on Windows Sep 3, 2019
@mikeblas
Copy link

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

@jstiops
Copy link

jstiops commented Mar 21, 2021

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"

@srebhan
Copy link
Contributor

srebhan commented May 8, 2023

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

@srebhan srebhan closed this as completed May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/exec bug unexpected problem or unintended behavior platform/windows
Projects
None yet
Development

No branches or pull requests