0

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": "username@user.com",
      "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.

Danish
  • 73
  • 10

3 Answers3

1

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
}
Danish
  • 73
  • 10
0

The Jenkins job succeeded because PSExec.exe returned exit code 0, which means that no errors were encountered. Jenkins jobs will fail if the underlying scripts fail (e.g. returning non-zero exit codes, like 1). If the PSExec.exe application isn't doing what you want it to - I would wrap it in another script which performs post-deploy validation, and returns 1 if the deployment failed.

See How/When does Execute Shell mark a build as failure in Jenkins? for more details.

alex
  • 6,818
  • 9
  • 52
  • 103
  • So how to make it show error i.e., it should return exit code 1 if any error occurs? – Danish Sep 18 '19 at 06:39
  • @Danish See https://devblogs.microsoft.com/powershell/windows-powershell-exit-codes/ – alex Sep 18 '19 at 13:25
  • It is working for normal powershell commands. I have even tried using `-ErrorAction Stop` but that is working for normal powershell commands but not with azure commands like `az login -u "username" -p "password"` – Danish Sep 19 '19 at 05:20
0

You can use powershell step, this should hand out the error directly as well.

Holleoman
  • 261
  • 1
  • 7