Param ( [string]$sfVersion = "8.2.1486.9590", #[Parameter(Mandatory=$True)] #$clusterConnection, #[Parameter(Mandatory=$True)] [int]$WaitTimeBetweenUpgradeChecksInSecs =60, #[Parameter(Mandatory=$True)] [int]$NumberOfTimesToCheck =10, [string]$serviceFabricDownloadURL="https://go.microsoft.com/fwlink/?linkid=839354" ) Write-Host "Service Fabric Cluster package will be downloaded from $serviceFabricDownloadURL" $fileLocation="$PSScriptRoot\Downloads\" Write-Host "Service Fabric package will be paced in $fileLocation" $sfCabFileName="MicrosoftAzureServiceFabric.$($sfVersion).cab" $sfFileName="$($fileLocation)$($sfCabFileName)" $extractTo=$fileLocation $retryCount = 40 $secondsToSleep=5 try { Write-Host "Attempting to connect to cluster" -ForegroundColor Yellow Write-Output "Start Connecting to the Cluster" $clusterSPN = 'http/SF-DEV02/bluebayinvest.com' Connect-ServiceFabricCluster -ConnectionEndpoint 'localhost:19000' -WindowsCredential -ClusterSpn $clusterSPN # Connect-ServiceFabricCluster @clusterConnection $UpgradeResult=Get-ServiceFabricClusterUpgrade If($UpgradeResult.TargetCodeVersion -eq $sfVersion) { Write-Host "Cluster is already in version $sfVersion" -ForegroundColor Green } Else { Write-Host "Path where the package will be downloaded - $fileLocation" -ForegroundColor Green Write-Host "Checking if the download folder is empty" -ForegroundColor Yellow IF (Test-Path $fileLocation) { Write-Host "Download folder is not empty. Attempting cleanup of $fileLocation" -ForegroundColor Yellow Remove-Item "$fileLocation\\*.*" Write-Host "Download cleanup complete" -ForegroundColor Green } Else { Write-Host "Download folder not present. Creating one" -ForegroundColor Yellow New-Item -ItemType Directory -Force -Path $fileLocation Write-Host "Download folder created" -ForegroundColor Green } Write-Host "Attempting download of Service Fabric package to $sfFileName" -ForegroundColor Yellow Invoke-WebRequest $serviceFabricDownloadURL -OutFile $sfFileName Write-Host "Download of Service Fabric package complete" -ForegroundColor Green If (Test-Path "$($sfFileName)") { Write-Host "Attempting copy of package to fabric image store" -ForegroundColor Yellow Copy-ServiceFabricClusterPackage -Code -CodePackagePath "$($sfFileName)" -ImageStoreConnectionString "fabric:ImageStore" Write-Host "Attempting to register package with service fabric" -ForegroundColor Yellow Register-ServiceFabricClusterPackage -Code -CodePackagePath $sfCabFileName Write-Host "Starting service fabric upgrade" -ForegroundColor Yellow Start-ServiceFabricClusterUpgrade -Code -CodePackageVersion $sfVersion -Monitored -FailureAction Rollback For($Count=0;$Count -le $NumberOfTimesToCheck ;$Count++) { Write-Host "Attempt number: $Count" -ForegroundColor Yellow $UpgradeResult=Get-ServiceFabricClusterUpgrade $UpgradeResult If(($UpgradeResult.TargetCodeVersion -eq $sfVersion) -and (( $UpgradeResult.UpgradeState -eq "RollingForwardCompleted"))) { Write-Host "Upgrade Completed" -ForegroundColor Green Break; } Else { Write-Host "Upgrade InProgress" -ForegroundColor Yellow } Start-Sleep -s $WaitTimeBetweenUpgradeChecksInSecs If($Count -ge $NumberOfTimesToCheck) { Write-Host "Wait time exhausted and Service fabric Cluster Upgrade is still InProgress. Verify manually" -ForegroundColor Yellow } } } Else { Write-Host "$sfCabFileName file is missing in $extractTo" -ForegroundColor Red } } } catch { $_.Exception Throw }