function Remove-AlkamiServiceFabricMultipleMajorVersions { <# .SYNOPSIS Removes multiple major versions of a microservice, preserving the latest major version. .PARAMETER hostname A single server hostname from the target Service Fabric cluster. #> [CmdletBinding()] Param( [Parameter(Mandatory = $false)] [string]$hostname = "localhost" ) $loglead = (Get-LogLeadName); Write-Host "$loglead Now removing multiple major microservice versions and keeping the latest version."; # Connect to the cluster and grab the deployed applications. Connect-AlkamiServiceFabricCluster -hostname $hostname | Out-Null; $applications = Get-AlkamiServiceFabricApplications -ComputerName $hostname; # Map applications from [packageName -> list of running applications] $appMap = @{}; foreach($app in $applications) { if(!($appMap.ContainsKey($app.Name))) { $appMap[$app.Name] = @(); } $appMap[$app.Name] = $appMap[$app.Name] + $app; } # Foreach application running on the cluster... $removedAny = $false; foreach($key in $appMap.Keys) { # Grab the services for the particular app running on the cluster. # Sort by name, which also sorts by major version. $services = $appMap[$key]; $services = $services | Sort-Object -Property ServiceFabricApplicationName; # If there is more than one version of the service running.. if($services.Count -gt 1) { # Remove everything except for the last service in the list. $removeCount = $services.Count - 1; for($i = 0; $i -lt $removeCount; $i++) { Write-Host "$loglead Removing $($services[$i].ServiceFabricApplicationName)"; Remove-ServiceFabricApplication -ApplicationName ($services[$i].ServiceFabricApplicationName) -Force; $removedAny = $true; } Write-Host "$loglead $($services[$removeCount].ServiceFabricApplicationName) still lives!`n"; } } if(!$removedAny) { Write-Host "$loglead Found no microservices to remove."; } }