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

Elastic-Agent: Do not output to STDERR under powershell, unless you want PS to fail execution as an error #118

Open
ThorbenJ opened this issue Nov 24, 2020 · 7 comments
Labels
8.9-candidate bug Something isn't working Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team

Comments

@ThorbenJ
Copy link

  • What: elastic-agent.exe
  • Version: 7.10.0 (but all previous too)
  • OS: Windows 10 (as of 2020-11-24)

Problem:

In certain execution contexts PowerShell will convert any line of text sent to STDERR into an Error object. This will no doubt go unhandled thus the commend is failed by powershell:

 PS C:\Users\Administrator\Documents\EC_Spout> C:\Users\Administrator\Documents\EC_Spout\agent_install+enroll.ps1
Uninstalling existing
Elastic Agent has been uninstalled.
The Elastic Agent is currently in BETA and should not be used in production

elastic-agent.exe : 2020-11-24T07:32:24.902-0800	DEBUG	kibana/client.go:170	Request method: POST, path: 
/api/fleet/agents/enroll
At C:\Users\Administrator\Documents\EC_Spout\agent_install+enroll.ps1:91 char:1
+ & "$download_dir\elastic-agent-$stack_ver-windows-x86_64\elastic-agen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (2020-11-24T07:3...t/agents/enroll:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError 

Google "PS NativeCommandError" to discover all the happy people that trip over this error.

Solution:
On Windows systems (especially under powershell) do not send anything to STDERR unless its really is an error, and the command should be terminated/failed.

Ways to reproduce:
In an Interactive PS, this error handling will most likely not be enabled. Under ISE is most often is.

Open PowerShell ISE and write a ps1 script:

 & "$download_dir\elastic-agent-7.10.0-windows-x86_64\elastic-agent.exe" install -f -k "$kn_url" -t "$agent_token" 

Run the script with the 'play' button in the toolbar (after saving it).

Doing it via ISE like this was the easiest way, I think, to have PS in such an error handling mode. I have experienced the same problem with PS scripts start by the task scheduler.

Extra info:
I maintain scripts to automate starting a demo env.: https://github.com/ElasticSA/ec_spout (more info for Elastic employees here: https://wiki.elastic.co/display/PRES/EC+Spout )

@elasticmachine
Copy link
Collaborator

Pinging @elastic/ingest-management (Team:Ingest Management)

@ph ph added the bug Something isn't working label Nov 24, 2020
@ph
Copy link
Contributor

ph commented Nov 24, 2020

@michalpristas or @blakerouse you know more windows than me. Can you take a look?

@ThorbenJ
Copy link
Author

Please note if you don't have $ErrorActionPreference = "Stop" (PS equiv of Bash's set -e), then PS will treat the STDERR output as an error and display it, but it wont immediately kill elastic-agent.exe. Tested under PowerShell ISE.
Why not under PowerShell interactive? I am no PS expert, but reading up on this it seems, ISE, PS Remote, and I assume task scheduled instances wrap STDERR with System.Management.Automation.ErrorRecord.

@jsoriano jsoriano added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Oct 29, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@michalpristas
Copy link
Contributor

closing, even with the options set i was not able to repro
Win 11 64bit

@ThorbenJ
Copy link
Author

ThorbenJ commented Nov 3, 2021

image

As I document you will not trip this issue in a normal powershell console. This error handling is only on by default in PS Remote, PS ISE and certain ways of scheduling PS scripts. Search for System.Management.Automation.ErrorRecord - If this error handling is enabled any line sent to STDERR becomes an ErrorRecord that needs to be handled. If not you get the NativeCommandError, and with $ErrorActionPreference = "Stop" the exe is stopped/killed.

 [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19042  0       

@ThorbenJ ThorbenJ reopened this Nov 3, 2021
@jlind23
Copy link
Contributor

jlind23 commented Nov 3, 2021

@michalpristas could you please take a look again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.9-candidate bug Something isn't working Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team
Projects
None yet
Development

No branches or pull requests

8 participants