Powershell 和服务结构 - 如何 select 特定信息并在以后的操作中使用它?

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

下面是我如何连接到我的服务结构集群并获取所需的应用程序运行状况信息的代码:

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

但是目前有很多资料,我不需要的。如何 select 从结果中获取特定信息?我只需要 ServiceHealthState 信息。我能够从应用程序运行状况中排除一些信息,但未能排除更多信息。

更新:

我找到了以下解决方案,它接近于我尝试实现的目标:

$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"
}

但是我不需要监控AggregatedHealthState,因为我只对ServiceHealthStates下的一部分感兴趣,看例子:

ApplicationName: fabric:/Myapp
AggregatedHealthState: Error
ServiceHealthStates:

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

但是我不知道如何select这部分,也没有从Google找到这个,希望有人能帮忙。

找到适合我的解决方案,post也在这里,如果其他人面临同样的问题,我的解决方案:

# 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
   }