Restart A Service Fabric Application Replica using PowerShell

less than 1 minute read

Tags: , ,

Categories:

Updated:

Here is a PowerShell script to restart replicas where specific applications / services are deployed to.


Connect-ServiceFabricCluster -ConnectionEndpoint <domain-without-https.com:19000> `
-KeepAliveIntervalInSec 10 -AzureActiveDirectory -ServerCertThumbprint <thumbprint>

$nodes = Get-ServiceFabricNode

For ($i = 0; $i -lt $nodes.Count; $i++) {

    $result =  Get-ServiceFabricDeployedReplica -NodeName $nodes[$i].NodeName -ApplicationName fabric:<appName> `
                | Where-Object ServiceName -eq fabric:<serviceName> | Select-Object -Property ReplicaId, Partitionid

    For ($j = 0; $j -lt $result.Count; $j++) {
        Write-Host $nodes[$i].NodeName $result[$j].Partitionid $result[$j].ReplicaId
        Restart-ServiceFabricReplica -NodeName  $nodes[$i].NodeName -PartitionId $result[$j].Partitionid -ReplicaOrInstanceId $result[$j].ReplicaId
    }
}