Search code examples
powershellazure-service-fabric

Powershell and service fabric - how to select specific information and use it later actions?


Below is code how I connect to my service fabric cluster and take needed application health information:

$ConnectArgs = @{
 ConnectionEndpoint = "mycluster:19000";  
 X509Credential = $True;  
 StoreLocation = 'CurrentUser';  
 StoreName = "MY"; 
 FindType = 'FindByThumbprint';  
 FindValue = "My_thumbprint"
 }
Connect-ServiceFabricCluster @ConnectArgs 
Get-ServiceFabricApplicationHealth -ApplicationName fabric:/Myapp -ExcludeHealthStatistics 

But currently there are lot of information, what I do not need. How to select just specific information from results? I need just ServiceHealthState information. I was able to exclude some information from application health, but didnt manage to exclude more.

UPDATE:

I found the following solution, which is close to what I try to achieve:

$health = Get-ServiceFabricApplicationHealth -ApplicationName "fabric:/Myapp" 
If ($health.AggregatedHealthState -eq "OK") {
    Write-Host "$($clusterApplication)'s health is ok!"
} Else {
    Write-Error "$($clusterApplication)'s health is not ok"
}

But I do not need to monitor AggregatedHealthState, because I am only interested in one part, which is under ServiceHealthStates, see example:

ApplicationName: fabric:/Myapp
AggregatedHealthState: Error
ServiceHealthStates:

ServiceName: fabric:/Myapp/Frontend
AggregatedHealthState: Error
ServiceName: fabric:/Myapp/Backend
AggregatedHealthState : Ok

But I dont know how to select this part, didnt find this from Google as well, hopefully someone could help.


Solution

  • Find solution what suits to me, post it here as well, if anyone else is facing against same question, my solution:

    # Connect to Service Fabric cluster
    $ConnectArgs = @{
     ConnectionEndpoint = "mycluster:19000";  
     X509Credential = $True;  
     StoreLocation = 'CurrentUser';  
     StoreName = "MY"; 
     FindType = 'FindByThumbprint';  
     FindValue = "My_Thumbprint"
     }
    Connect-ServiceFabricCluster @ConnectArgs 
    
    # Test Cluster connection
    $Connection=Test-ServiceFabricClusterConnection
    If ($Connection -eq "True") { 
        Write-Host "OK"}
    Else 
    {
        Write-Host "Connection to Service fabric cluster failed" 
    }
    
    # Service Fabric application health monotoring
    $AppName="MyApp"
    
    $AppHealth=Get-ServiceFabricApplicationHealth -ApplicationName $AppName 
    $AppHealthBE=$AppHealth.ServiceHealthStates -match "Backend"
    
    $node=Get-ServiceFabricApplicationHealth -ApplicationName $AppName
    $nodetorestart=$node.DeployedApplicationHealthStates.NodeName -match  "MyNode"
    
    If ($AppHealthBE.AggregatedHealthState -eq "Ok") {
       Write-Host "$($clusterApplication)/backend is OK"
       }
       Else
       {
       Write-Host "Need to perform restart on node: $nodetorestart)"
       #Restart-ServiceFabricNode -NodeName $nodetorestart
       }