Search code examples
azurepowershelljenkinspsexec

Using PsExec in Jenkins, even if the script fails, it shows Success


I am trying to run a powershell script which first logins to azure and then deploys the zip file to azure using psexec.

I am using the following command:

F:\jenkins\VMScripts\PsExec64.exe \\WINSU9 -u "WINSU9\administrator" -p mypassword /accepteula -h PowerShell -noninteractive -File C:\Shared\Trial\webappscript.ps1

I am getting the output as:

PsExec v2.2 - Execute processes remotely
Copyright (C) 2001-2016 Mark Russinovich
Sysinternals - www.sysinternals.com

[
  {
    "cloudName": "AzureCloud",
    "id": "a7b6d14fddef2",
    "isDefault": true,
    "name": "subscription_name",
    "state": "Enabled",
    "tenantId": "b41cd",
    "user": {
      "name": "[email protected]",
      "type": "user"
    }
  }
]
WARNING: Getting scm site credentials for zip deploymentConnecting to WINSU9...


Starting PSEXESVC service on WINSU9...


Connecting with PsExec service on WINSU9...


Starting PowerShell on WINSU9...



PowerShell exited on WINSU9 with error code 0.
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

It is just giving the output of az login command but the output of deployment is not showing. Also if the deployment fails, it will still show success. But it should show failure.


Solution

  • Answering my question so that others facing the same issue can get help here.
    As @Alex said that powershell is exiting with error code 0, I tried to return the error code 1 whenever any command fails. Since the output of Azure CLI is in json format, I stored that output in a variable and checked if it contains anything. The sample of the code is written below.

    $output = az login -u "username" -p "password" | ConvertFrom-Json
    if (!$output) {
        Write-Error "Error validating the credentials"
        exit 1
    }