ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Remove-AlkamiServiceFabricCluster.ps1
2023-05-30 22:51:22 -07:00

62 lines
2.1 KiB
PowerShell

function Remove-AlkamiServiceFabricCluster {
<#
.SYNOPSIS
Shuts down a service fabric cluster and removes it completely.
.PARAMETER hostname
A single server hostname from the target Service Fabric cluster.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$hostname = "localhost",
[Parameter(Mandatory = $false)]
[switch]$WhatIf
)
$loglead = Get-LogLeadName;
if($WhatIf.IsPresent) {
Write-Host "$loglead : Running in WhatIf mode. The cluster is not actually being destroyed!";
}
Write-Host "$loglead : Destroying Service Fabric cluster.";
Write-Host "$loglead : Connecting to Service Fabric cluster.";
# Connect to SF cluster.
Connect-AlkamiServiceFabricCluster -hostname $hostname | Out-Null;
# TODO: Handle when the cluster is already destroyed.
# Query all of the nodes from the cluster we're destroying.
Write-Host "$loglead : Querying sibling nodes from $hostname.";
$nodes = (Get-ServiceFabricNode);
$names = ($nodes | select-object -ExpandProperty IpAddressOrFQDN) -join ", ";
Write-Host "$loglead : Found node(s) $names";
# Remove the cluster.
Write-Host "$loglead : Shutting down Service Fabric cluster.";
$clusterConfigPath = "c`$\ProgramData\SF\ClusterConfig.json";
$configPath = "\\$hostname\$clusterConfigPath";
if($WhatIf.IsPresent) {
Write-Host "$loglead : Mwahaha! The cluster running on $hostname and friends would have been destroyed!";
} else {
Remove-ServiceFabricCluster -ClusterConfigurationFilePath $configPath -Force;
}
# The object returned can't be enumerated by foreach for some reason.
foreach($node in $nodes) {
$server = $node.IpAddressOrFQDN;
$nodeName = $node.NodeName;
$sfPath = "\\$server\C`$\ProgramData\SF\$nodeName";
if(Test-Path $sfPath) {
Write-Host "$loglead : Removing Fabric data at `"$sfPath`"";
if(!($WhatIf.IsPresent)) {
Remove-Item $sfPath -Recurse -Force;
}
}
}
}