Service Fabric Start-ServiceFabricApplicationUpgrade - 如何让 powershell 在继续之前等待升级成功

Service Fabric Start-ServiceFabricApplicationUpgrade - How to make powershell wait for successful upgrade before continuing

我正在放弃使用 Azure devops 管道“升级 Service Fabric 应用程序”任务,并将其替换为纯 PowerShell 方法。

Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback

问题是当我使用 devops 管道任务时,它会等待升级完成并在控制台中显示输出 window。当我使用我的自定义 powershell 时,它会立即执行并移动到下一个管道任务,即使在我的集群中我可以看到升级刚刚开始。如何让它显示输出并等待成功再继续?

此命令Get-Service Fabric Application Upgrade将获取 Service Fabric 应用程序升级的状态,因此我们可以在以下 PowerShell 脚本中使用它来获取此升级的结果。

## Start monitored application upgrade
try
{
    Write-Host "Start upgrading application..." 
    Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback
}
catch
{
    Write-Host ("Error starting upgrade. " + $_)

    Write-Host "Unregister application..."
    Unregister-ServiceFabricApplicationType -ApplicationTypeName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -Force
    throw
}

do
{
    Write-Host "Waiting for upgrade..."
    Start-Sleep -Seconds 3
    $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName)
} while ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
    
if($upgradeStatus.UpgradeState -eq "RollingForwardCompleted")
{
    Write-Host "Upgrade completed successfully."
}
elseif($upgradeStatus.UpgradeState -eq "RollingBackCompleted")
{
    Write-Error "Upgrade was Rolled back."
}
elseif($upgradeStatus.UpgradeState -eq "Failed")
{
    Write-Error "Upgrade Failed."
}

因此,此 PowerShell 任务将等待此升级的结果,然后再移动到下一个管道任务。

尝试类似下面的操作,它将等待脚本块完成并 运行 下一行。

$job = Start-Job -ScriptBlock{
    Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback
} 
Wait-Job $job.Name