There are ways to make this more efficient, and to also handle those times when grpcurl doesn't return anything in STDOUT (e.g. the host cannot be contacted, authentication fails, etc.) but writes an error message to STDERR, but I'll leave that part up to you,
Give this one a try:
function Serving{
param(
$val
)
if ($val -isnot [array]){
return $false
}
if ($val.count -ne 3){
return $false
}
Try{
$p = $val -join "" | ConvertFrom-Json -ErrorAction STOP
if ($p.status -eq 'serving'){
$true
}
else{
$false
}
}
Catch{
Write-Host "'$p' is not JSON"
$false
}
}
while ($true) {
$beep = $false
[array]$response = grpcurl -d '{ \"service1\": \"name1\" }' -H "authorization: key key" host1:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service1 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service2\": \"name2\" }' -H "authorization: key key" host2:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service2 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service3\": \"name3\" }' -H "authorization: key key" host3:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service3 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service4\": \"name4\" }' -H "authorization: key key" host4:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service4 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service5\": \"name5\" }' -H "authorization: key key" host5:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service5 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service6\": \"name6\" }' -H "authorization: key key" host6:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service6 isn't serving" -ForegroundColor Yellow
$beep = $true
}
[array]$response = grpcurl -d '{ \"service7\": \"name7\" }' -H "authorization: key key" host7:port grpc.health.v1.Health/Check
if (-Not (Serving $response)){
Write-Host "Service7 isn't serving" -ForegroundColor Yellow
$beep = $true
}
if ($beep){
[console]::beep(500, 300)
}
Start-Sleep 2
}