title: Get exit code from `az vm run-command` in Azure pipeline
body: <p>I'm running a rather hefty build in my Azure pipeline, which involves processing a large amount of data, and hence requires too much memory for my buildagent to handle. My approach is therefore to start up an linux VM, run the build there, and push up the resulting docker image to my container registry.</p>
  <p>To achieve this, I'm using the <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-cli?view=azure-devops" rel="noreferrer">Azure CLI task</a> to issue commands to the VM (e.g. <code>az vm start</code>, <code>az vm run-command ...</code> etc).</p>
  <p>The problem I am facing is that <code>az vm run-command</code> &quot;succeeds&quot; even if the script that you run on the VM returns a nonzero status code. For example, this &quot;bad&quot; vm script:</p>
  <pre><code>az vm run-command invoke -g &lt;group&gt; -n &lt;vmName&gt; --command-id RunShellScript --scripts &quot;cd /nonexistent/path&quot;
  </code></pre>
  <p>returns the following response:</p>
  <pre><code>{
    &quot;value&quot;: [
      {
        &quot;code&quot;: &quot;ProvisioningState/succeeded&quot;,
        &quot;displayStatus&quot;: &quot;Provisioning succeeded&quot;,
        &quot;level&quot;: &quot;Info&quot;,
        &quot;message&quot;: &quot;Enable succeeded: \n[stdout]\n\n[stderr]\n/var/lib/waagent/run-command/download/87/script.sh: 1: cd: can't cd to /nonexistent/path\n&quot;,
        &quot;time&quot;: null
      }
    ]
  }
  </code></pre>
  <p>So, the command succeeds, presumably because it succeeded in executing the script on the VM. The fact that the script actually failed on the VM is buried in the response &quot;message&quot;</p>
  <p>I would like my Azure pipeline task to fail if the script on the VM returns a nonzero status code. How would I achieve that?</p>
  <p>One idea would be to parse the response (somehow) and search the text under <code>stderr</code> - but that sounds like a real hassle, and I'm not sure even how to &quot;access&quot; the response within the task.</p>