Answers: <p>I found out how to do it without pipeline. It only has to do with command line script:</p>
  
  <pre><code>start "" C:\MyApp\MyApp.exe &gt;&gt; text.txt
  </code></pre>
  
  <p>This call is asynchronous (i.e., the command line won't wait for MyApp.exe to return). If you want it to wait, you can use the /w flag, like this:</p>
  
  <pre><code>start /w "" C:\MyApp\MyApp.exe &gt;&gt; text.txt
  </code></pre>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-05T10:51:18.180Z
Answers: 2017-12-05T12:58:48.480Z
Answers: 2017-12-05T12:58:48.480Z
Answers: Noam Ohana
Body: <p>I'm trying to execute an exec file from a jenkins server, using windows cmd. This is what is run from jenkins Windows batch window:</p>
  
  <p><code>C:\MyApp\MyApp.exe</code></p>
  
  <p>When run regularly from command line, the app prints to the console reporting its progress.</p>
  
  <p>When I run it from jenkins, I get the following exception (MyApp stands for the name of my actual application):</p>
  
  <pre><code>08:13:56 Started by user NO
  08:13:56 Building remotely on SFM BSP builder (ComputerName) (projectName) in workspace C:\Jenkins\workspace\MyBuildMachine
  08:13:56 Running Prebuild steps
  08:13:56 [MyBuildMachine] $ cmd /c call C:\Windows\TEMP\jenkins3066529473565168228.bat
  08:13:56 
  08:13:56 C:\Jenkins\workspace\MyBuildMachine&gt; C:\Users\NO\Desktop\MyApp.exe 
  08:13:56 
  08:13:56 Unhandled Exception: System.IO.IOException: The handle is invalid.
  08:13:56 
  08:13:56    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  08:13:56    at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean&amp; succeeded)
  08:13:56    at MyApp.App.AppInit(String[] args)
  08:13:56    at MyApp.Program.Main(String[] args)
  08:13:56 
  08:13:56 C:\Jenkins\workspace\MyBuildMachine&gt;exit 255 
  08:13:56 Build step 'Execute Windows batch command' marked build as failure
  08:13:56 Email was triggered for: Failure - Any
  08:13:56 Sending email for trigger: Failure - Any
  08:13:56 Sending email to: myEmail@email.com
  08:13:58 Finished: FAILURE
  </code></pre>
  
  <p>Needless to say, the app didn't do its job.
  Trying to understand the problem, I found <a href="https://stackoverflow.com/questions/27749674/why-does-adjusting-console-buffer-raise-invalid-handle-exception-when-redirectin">here</a> an explanation about stdout redirection, and Jenkins actually does that automatically, which causes this exception. And indeed also when I run it in cmd with stdout redirection, i.e. <code>C:\&gt; MyApp.exe &gt;&gt; appOut.txt</code>, it shows the same error.</p>
  
  <p>I have no access to the source code of the app. I know that it could be solved from inside. My question is: Is there any way to prevent Jenkins from redirecting the stdout to a file, or wrapping the exec somehow such that will allow me to run it? (of course, running it from another exec and just catching the exception won't work. There won't be an exception but also the app won't run).</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-11-29T16:32:04.297Z
Comments: Does the command run on the jenkins box as well when you ssh to it?
Comments: 030
Tags: <jenkins><windows><shell-script>
Title: Jenkins Exception that is caused by stdout redirection using windows cmd
UserDisplayName: Noam Ohana
ThreadText: # Jenkins Exception that is caused by stdout redirection using windows cmd
  
  (Asked by: username_0 on 2017-11-29)
  
  I'm trying to execute an exec file from a jenkins server, using windows cmd. This is what is run from jenkins Windows batch window:
  
  `C:\MyApp\MyApp.exe`
  
  When run regularly from command line, the app prints to the console reporting its progress.
  
  When I run it from jenkins, I get the following exception (MyApp stands for the name of my actual application):
  
  ```
  08:13:56 Started by user NO
  08:13:56 Building remotely on SFM BSP builder (ComputerName) (projectName) in workspace C:\Jenkins\workspace\MyBuildMachine
  08:13:56 Running Prebuild steps
  08:13:56 [MyBuildMachine] $ cmd /c call C:\Windows\TEMP\jenkins3066529473565168228.bat
  08:13:56 
  08:13:56 C:\Jenkins\workspace\MyBuildMachine> C:\Users\NO\Desktop\MyApp.exe 
  08:13:56 
  08:13:56 Unhandled Exception: System.IO.IOException: The handle is invalid.
  08:13:56 
  08:13:56    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  08:13:56    at System.Console.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
  08:13:56    at MyApp.App.AppInit(String[] args)
  08:13:56    at MyApp.Program.Main(String[] args)
  08:13:56 
  08:13:56 C:\Jenkins\workspace\MyBuildMachine>exit 255 
  08:13:56 Build step 'Execute Windows batch command' marked build as failure
  08:13:56 Email was triggered for: Failure - Any
  08:13:56 Sending email for trigger: Failure - Any
  08:13:56 Sending email to: myEmail@email.com
  08:13:58 Finished: FAILURE
  
  ```
  
  Needless to say, the app didn't do its job.
  Trying to understand the problem, I found [here](https://stackoverflow.com/questions/27749674/why-does-adjusting-console-buffer-raise-invalid-handle-exception-when-redirectin) an explanation about stdout redirection, and Jenkins actually does that automatically, which causes this exception. And indeed also when I run it in cmd with stdout redirection, i.e. `C:\> MyApp.exe >> appOut.txt`, it shows the same error.
  
  I have no access to the source code of the app. I know that it could be solved from inside. My question is: Is there any way to prevent Jenkins from redirecting the stdout to a file, or wrapping the exec somehow such that will allow me to run it? (of course, running it from another exec and just catching the exception won't work. There won't be an exception but also the app won't run).
  
  **Tags:** jenkins, windows, shell-script
  
  > **Comments:**
  > **username_1**: Does the command run on the jenkins box as well when you ssh to it?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-12-05. Score: 2 | Accepted answer
  
  I found out how to do it without pipeline. It only has to do with command line script:
  
  ```
  start "" C:\MyApp\MyApp.exe >> text.txt
  
  ```
  
  This call is asynchronous (i.e., the command line won't wait for MyApp.exe to return). If you want it to wait, you can use the /w flag, like this:
  
  ```
  start /w "" C:\MyApp\MyApp.exe >> text.txt
  
  ```
  
  
  ---
  
  