gpt4 book ai didi

powershell - 从输出中删除 @{}

转载 作者:行者123 更新时间:2023-12-02 23:33:37 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I write the value of a single property of a object?

(2 个回答)


去年关闭。




以下代码的输出类似于@{Average = 2}。如何在没有@{} 的情况下获得Average = 2 的输出。

$cpu = $ENV:COMPUTERNAME |Foreach-Object {
Get-WmiObject -computername $_ win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
}

最佳答案

首先,我认为你不需要管道 $env:COMPUTERNAMEForeach-Object , 因为它的类型是 System.String , 不是数组或任何其他类型的集合。使用会更容易 -ComputerName $env:COMPUTERNAME直接地。您可以使用 ($env:COMPUTERNAME).GetType() 查看类型是什么.也看看 about_environment_variables 有关 PowerShell 中 Windows 环境变量的更多信息。

其次,如@Mathias R. Jessen在评论中建议,你应该使用 -ExpandProperty扩大属性(property)@{Average = 2}2 .

修改命令

Get-WmiObject -ComputerName $env:COMPUTERNAME -Class Win32_Processor `
| Measure-Object -Property LoadPercentage -Average `
| Select-Object -ExpandProperty Average

您也可以运行 Get-Help Select-Object -Parameter ExpandProperty看什么 ExpandProperty适用于 Select-Object .
-ExpandProperty <String>
Specifies a property to select, and indicates that an attempt should be made to expand that property. Wildcards are permitted in the property name.

For example, if the specified property is an array, each value of the array is included in the output. If the property contains an object, the properties of that object are displayed in the output.

Required? false
Position? named
Default value None
Accept pipeline input? False
Accept wildcard characters? false

也作为 @mklement0 在评论中提到的旁注,WMI cmdlet(例如 Get-WmiObject )已被 PowerShell v3(2012 年 9 月发布)中的 CIM cmdlet(例如 Get-CimInstance)取代。当您运行 Get-Help Get-WmiObject 时,也会向您指出这一点。 :

Starting in Windows PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance



也在这个 Use CIM cmdlets not WMI cmdlets文章。另一个原因是 future 是 PowerShell Core,它不再支持 WMI cmdlet。你可以看看这个 answer想要查询更多的信息。

综上所述,这里是使用 Get-CimInstance 的等效 CIM 命令。 .
Get-CimInstance -ClassName Win32_Processor `
| Measure-Object -Property LoadPercentage -Average `
| Select-Object -ExpandProperty Average

这将适用于 Windows PowerShell 和 PowerShell Core。

关于powershell - 从输出中删除 @{},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60832423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com