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.

4

2 回答 2

1

exit $LASTEXITCODE对我有用(如果失败的命令是前一个命令)

于 2017-11-29T03:00:25.977 回答
0

我找到了另一种解决方法。

PowerShell 中的大多数错误都会引发异常。由于未知原因,Bamboo 在执行内联脚本时似乎没有考虑到这一点。

我们可以通过自己的内联异常捕获来解决这个问题,例如:

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

有了这个,我能够正确地部署失败。上述 $error[0] 的内联检查也有效,但可能并非所有例外。

于 2016-02-15T19:46:51.570 回答