使用Get-Service返回对象中的服务器名称

huangapple go评论74阅读模式
英文:

Return a server name within an object using Get-Service

问题

I can provide the translated code portion:

我正在尝试引入一些额外的健康监控来监视我们的Elasticsearch实例其中一部分是查看Elasticsearch服务是否正在运行

其中一部分是返回服务器的主机名然后是Elasticsearch服务的显示名称和状态最好作为PSObject这样我可以根据需要选择单个属性

所以我有一个可以从中提取Get-Service信息的服务器数组

$servers = @("server1","server2","server3")
$services = Get-Service Elasticsearch* -ComputerName $servers | Select -Property DisplayName, Status
这似乎无法为我提供包括主机名的方法所以我尝试循环遍历服务器数组

$servers = @("server1","server2","server3")
$services = foreach($i in $servers){get-service Elasticsearch* -ComputerName $i | Select @{Name='Hostname';Expression={$i}}, DisplayName, Status}

这使我得到了一个名为"server1"的PSObject属性,但没有值,这是我预期的,但我无法理解如何在循环期间声明属性,然后在循环期间添加值。


<details>
<summary>英文:</summary>

I&#39;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&#39;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&#39;t seem to give me a way forward to include the hostname. So I&#39;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 &quot;server1&quot; with no value against it, which I guess I expected but I can&#39;t fathom how to declare the property and then add the values in during the loop.


</details>


# 答案1
**得分**: 1

> ... 但我无法理解如何声明属性,然后在循环中添加值

有助于此!about_Calculated_Properties帮助主题详细解释了如何构建所谓的计算属性。

您需要提供一个哈希表,其中包含Name(或Label)的条目,以及用于计算结果属性值的Expression的条目:

$servers = @(&quot;server1&quot;,&quot;server2&quot;,&quot;server3&quot;)
$services = foreach($i in $servers){
  Get-Service Elasticsearch* -ComputerName $i | Select @{Name=&#39;Server&#39;;Expression={$i}}, DisplayName, Status
}

<details>
<summary>英文:</summary>

&gt; ... but I can&#39;t fathom how to declare the property and then add the values in during the loop

There&#39;s help for that! The [`about_Calculated_Properties` help topic](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_calculated_properties) 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
}


</details>



huangapple
  • 本文由 发表于 2023年5月10日 23:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定