I'm trying to introduce some extra health monitoring of our Elasticsearch instances, part of which is seeing whether the Elasticsearch service is running.
Part of this would be to return the Hostname of the servers, then the DisplayName and Status of the Elasticservice and preferably as a PSObject so I can select individual properties as needed.
So I've got an array of servers which I can pull Get-Service info from:
$servers = @("server1","server2","server3")
$services = Get-Service Elasticsearch* -ComputerName $servers | Select -property DisplayName, Status
This doesn't seem to give me a way forward to include the hostname. So I've tried to loop through the server array instead.
$servers = @("server1","server2","server3")
$services = foreach($i in $servers){get-service Elasticsearch* -ComputerName $i | Select $i, DisplayName, Status}
This got me a PSObject property called "server1" with no value against it, which I guess I expected but I can't fathom how to declare the property and then add the values in during the loop.
... but I can't fathom how to declare the property and then add the values in during the loop
There's help for that! The about_Calculated_Properties
help topic explains exactly how to construct so-called calculated properties.
You need to supply a hashtable with an entry for the Name
(or Label
), and one for the Expression
which is used to calculate the resulting property value:
$servers = @("server1","server2","server3")
$services = foreach($i in $servers){
Get-Service Elasticsearch* -ComputerName $i | Select @{Name='Server';Expression={$i}}, DisplayName, Status
}