<p>I've problem running <code>docker login</code> against AWS ECR with Powershell. <br>More specifically I'm running it from a Jenkins pipeline on Windows container (inside a K8S cluster) using the <code>powershell</code> step as follow</p>
<pre><code>powershell &quot;aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin ****.dkr.ecr.eu-central-1.amazonaws.com&quot;
</code></pre>
<p>but it fails with following error:</p>
<pre><code>09:24:32  At C:\Jenkins\agent\workspace\test\awsIamRole-Test@tmp\durable-e2ffd0da\powershellWrapper.ps1:3 char:1
09:24:32  + &amp; powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
09:24:32  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
09:24:32      + CategoryInfo          : NotSpecified: (Error response ...400 Bad Request    :String) [], RemoteException
09:24:32      + FullyQualifiedErrorId : NativeCommandError
</code></pre>
<p>The strange behavior is that if I run the command <strong>manually</strong> on the container (both on my local machine and on the cluster) everything works fine and the login is successful. Below there's the container's Dockerfile.</p>
<pre><code># escape=`
FROM mcr.microsoft.com/windows/servercore:1809

SHELL [&quot;powershell&quot;] 

RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install jq docker -y 
RUN choco install awscli --version=2.1.15 -y
</code></pre>
<p>Since the container runs on an EC2 instance and I need to run Docker inside the container, I bind to Docker socket of underlying EC2 machine when launching the container on K8S, as shown below (it works since <code>docker ps</code> from the pipeline show the correct results).</p>
<pre><code>  - image: &quot;****.dkr.ecr.eu-central-1.amazonaws.com/jenkins-container-templates/docker-awscli2-windows:latest&quot;
    name: &quot;docker-awscli2-windows&quot;
    tty: true
    volumeMounts:
    - mountPath: &quot;\\\\.\\pipe\\docker_engine&quot;
      name: &quot;docker-pipe&quot;
  volumes:
  - hostPath:
      path: &quot;\\\\.\\pipe\\docker_engine&quot;
    name: &quot;docker-pipe&quot;
</code></pre>
<p>What could be the problem?</p>

<p>I managed to solve it. <br></p>
<h3> Problem cause </h3>
<p>It seems that problem is related to how pipes are managed when running powershell on Jenkins and for some reason (that I haven't figured out yet), a new line is added before the token, so that instead of</p>
<pre><code>token
</code></pre>
<p>the second part of the command get as input</p>
<pre><code>
token
</code></pre>
<p>and it probably process it like there is no input at all and the <code>--password-stdin</code> flag make it fail with <code>BadRequest</code> status.</p>
<h3> Workaround </h3>
<p>Since I was unable to remove this new line, I used as workaround</p>
<pre><code>powershell 'docker login --username AWS -p $(aws ecr get-login-password --region eu-central-1 ) ****.dkr.ecr.eu-central-1.amazonaws.com'
</code></pre>
