3

I am using below script to connect to remote server and shut the cluster service and then deploy packages. This is cluster service shut down script.

$SvcName = '${bamboo.ServiceName}'
$SvrNames = '${bamboo.deploy.hostname}'
#$SvcName = "'" + $SvcName + "'"
$SvrName = $SvrNames[0]
try {
  $services = Get-WmiObject -Computer $SvrName -Authentication PacketPrivacy -Namespace 'root\mscluster' MSCluster_Resource |
    Where {$_.Type -eq "Generic Service"} |
    Where {$_.name -eq $SvcName}
  if (-Not $Services) {
    $SvcName + " is not installed on this computer."
  } else {
    Switch($services.state) {
      '2' {
        Write-Host "Cluster service $SvcName is online"
        $SvcName = "'" + $SvcName + "'"
        $cmd  = "CLUSTER RES" + ' ' + $SvcName + ' ' + "/OFF"
        $cmd1 = [scriptblock]::Create($cmd)
        Invoke-Command -ComputerName $SvrName -ScriptBlock $cmd1
        Start-Sleep -s 10
        Write-Host "$SvcName is Offline"
      }
      '3' {
        Write-Host "Cluster service $SvcName is Offline"
        Write-Host $_.Exception
        Write-Host $_.Exception.Message
        Start-Sleep -s 10
        break 
      }
      '4'{
        Write-Host "Cluster service $SvcName is in Falied state, Please login to $SvrNames and check event logs"
        Start-Sleep -s 10
      }
    }
  }
} catch {
  $error[0].Exception
  Write-Host $_.Exception
  Write-Host $_.Exception.Message
  break
}

Why does Bamboo does not fail when there is a clear exception or an error message in the deploy logs?

Do I need to do something different here? $LASTEXITCODE doesn't work as well.

rambok
  • 49
  • 1
  • 5
  • Not every error is a terminating one, and even terminating errors may not affect the script depending on how the code where they occur is executed. What is the exact error you're getting, and at which point in your code do you get it? – Ansgar Wiechers Feb 05 '16 at 22:42
  • @ Ansgar Wiechers well, i am try to generate error so that the bamboo deployment project fails. i can see the exception in the logs like this. $services = Get-WmiObject -Computer $SvrName -Authentication PacketPrivacy -namespace '' in expression or statement. At C:\Users\svcSACpackages\AppData\Local\Temp\276594690-276758532-277446730-Scr iptBuildTask-985552963083952861.ps1:6 char:88 + ... cy -namespace 'root\mscluster' ` + ~~~~~~~~~~~~~~~~~ Unexpected token 'root\mscluster' ` MSCluster_Resource | Where {$_.Type -eq "Generic Service"} | Where {$_.name -eq $SvcName} – rambok Feb 08 '16 at 13:13
  • Please edit your question. – Ansgar Wiechers Feb 08 '16 at 13:15
  • this peice of code is making the bamboo deployment project fail. `if($error[0]) { Write-host $error[0] $LASTEXITCODE = 1 exit $lastexitcode } ` but i do not know if this is useful in all scenarios – rambok Feb 08 '16 at 13:17
  • Please. Edit. Your. Question. – Ansgar Wiechers Feb 08 '16 at 13:41
  • I have the exact same issues. All sorts of errors are slipping though, even failed module loading. Looking for a workaround – Eric Giguere Feb 15 '16 at 18:49

2 Answers2

1

I've found another work around.

Most of the errors in PowerShell throws exceptions. For and unknown reason, Bamboo doesn't seem to take that into account when executing inline scripts.

We can fix that by doing our own exception catching inline, like:

try 
{
    Call here some fancy PowerShell functions
}
catch
{
    Write-Host $_.Exception.GetType().FullName, $_.Exception.Message
    exit 500
    # or whatever code not 0
}

With that, I was able to have the deployment properly failed. The inline check for $error[0] stated above also works but maybe not will all exceptions.

Eric Giguere
  • 766
  • 5
  • 9
1

exit $LASTEXITCODE works for me (if the command which is failing is the previous command)

jmccure
  • 1,180
  • 7
  • 16