title: How to suppress stdout and stderr output for openssl command?
body: <p>Is it possible to disable the <code>stdout</code> and <code>stderr</code> output for the following command?</p>
  
  <pre><code>req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout ./private.key -out ./mycert.crt -subj "/C=DE/ST=BW/L=Karlsruhe/O=foobar/OU=foobar/CN=foobar"
  </code></pre>
  
  <p>The command always produces the following output which I would like to suppress:</p>
  
  <pre><code>Generating a 2048 bit RSA private key
  .............................................
  .............................................................+++
  ........................................+++
  writing new private key to './private.key'
  </code></pre>
  
  <p>I already tried to redirect the output with any variants of <code>2&gt;&amp;1</code> but his doesn't work in this scenario because of these reasons:</p>
  
  <p>The <code>openssl</code> command is executed within a powershell session like this:</p>
  
  <pre><code>$opensslCommand = 'req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout ./private.key -out ./mycert.crt -subj "/C=DE/ST=BW/L=Karlsruhe/O=foobar/OU=foobar/CN=foobar"'
  Invoke-Expression -Command $opensslCommand
  </code></pre>
  
  <p>Again, the redirection of the PowerShell streams doesn't work either.
  This wouldn't be a problem at all, but if your run the command in a non interactive mode the openssl output will be send to <code>stderr</code> and not to <code>stdout</code>.</p>
  
  <p><code>Git</code> behaves similar. But there you can change the output with these environment var: <code>$env:GIT_REDIRECT_STDERR = '2&gt;&amp;1'</code></p>
  
  <h2>Workaround</h2>
  
  <ul>
  <li>Callstack:</li>
  </ul>
  
  <blockquote>
    <p><code>Module Function</code> -- <em>calls</em> --> <code>Sub Function</code> -- <em>Invoke-Expression</em> --> <code>OpenSSL Command (with redirect *&gt;$null)</code></p>
  </blockquote>
  
  <ul>
  <li>Problem:</li>
  </ul>
  
  <p>The error occurs because the <em>Sub Function</em> ist called with the <code>ErrorAction 'Stop'</code>. If I change it to the default <code>Continue</code> the output is suppressed.</p>
  
  <p>To be still able to do a proper error handling I keep the global default <code>Error Action: Stop</code> and manually for the <code>Invoke-Command</code>:</p>
  
  <pre><code>$ErrorActionPreference = 'Continue'
  Invoke-Expression -Command 'openssl ... *&gt;$null'
  $ErrorActionPreference = 'Stop'
  </code></pre>