ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Remove-AlkamiServiceFabricApplications.ps1

65 lines
2.4 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Remove-AlkamiServiceFabricApplications {
<#
.SYNOPSIS
Removes/unregisters all applications from a service fabric cluster.
.PARAMETER hostname
A single server hostname from the target Service Fabric cluster.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$hostname = "localhost",
[Parameter(Mandatory = $false)]
[switch]$force
)
$loglead = Get-LogLeadName;
if(!($force.IsPresent)) {
throw "$loglead Must include -Force. You wouldn't want to run this accidentally, now would you?";
return;
}
# Connect to SF cluster.
Connect-AlkamiServiceFabricCluster -hostname $hostname | Out-Null;
# Attempt removing applications twice, because services that take too long to stop will time out the requests.
# The removals still happen, but there is no way to control the timeout on the command.
# Two attempts generally gets them all.
$removalAttempts = 2;
for($i = 0; $i -lt $removalAttempts; $i++) {
# Remove all applications.
$applications = Get-ServiceFabricApplication;
foreach($application in $applications) {
try {
Write-Host "$loglead Removing $($application.ApplicationName)-$($application.ApplicationTypeVersion)";
Remove-ServiceFabricApplication -ApplicationName $application.ApplicationName -Force;
}
catch {
Write-Warning $_;
}
}
# Unregister all applications.
$registeredApps = Get-ServiceFabricApplicationType;
foreach($application in $registeredApps) {
try {
Write-Host "$loglead Unregistering $($application.ApplicationTypeName)-$($application.ApplicationTypeVersion)";
Unregister-ServiceFabricApplicationType -ApplicationTypeName $application.ApplicationTypeName -ApplicationTypeVersion $application.ApplicationTypeVersion -Force;
}
catch {
Write-Warning $_;
}
}
}
# Throw an exception if the cluster was not completely wiped.
$applications = Get-ServiceFabricApplication;
$registeredApps = Get-ServiceFabricApplicationType;
if(($applications.count -gt 0) -or ($registeredApps.count -gt 0)) {
throw "$loglead The cluster was not completely wiped of applications.";
}
Write-Host "$loglead Service Fabric application removal complete.";
}