本文介绍了如何在 PowerShell 中计算对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在 PowerShell 用户指南中阅读的那样,PowerShell 的核心概念之一是命令接受并返回 对象 而不是文本.例如,运行 get-alias 会返回许多 System.Management.Automation.AliasInfo 对象:

PS Z:\> 获取别名命令类型名称定义——————————————————别名 % ForEach-Object别名?哪里-对象别名 ac 添加内容别名 asnp Add-PSSnapIn别名 cat Get-Content别名 cd 设置位置别名 chdir 设置位置...

现在,我如何获得这些对象的数量?

解决方案

这会让你数数:

get-alias |措施

您可以像处理对象一样处理结果:

$m = get-alias |措施$m.Count

如果你想在某个变量中也有别名,你可以使用 Tee-Object:

$m = get-alias |tee - 变量别名 |措施$m.Count$别名

有关 Measure-Object cmdlet 的更多信息位于 Technet.

不要将它与用于时间测量的 Measure-Command cmdlet 混淆.(再次在 Technet 上)

As I'm reading in the PowerShell user guide, one of the core PowerShell concepts is that commands accept and return objects instead of text. So for example, running get-alias returns me a number of System.Management.Automation.AliasInfo objects:

PS Z:\> get-alias

CommandType     Name                                             Definition
-----------     ----                                             ----------
Alias           %                                                ForEach-Object
Alias           ?                                                Where-Object
Alias           ac                                               Add-Content
Alias           asnp                                             Add-PSSnapIn
Alias           cat                                              Get-Content
Alias           cd                                               Set-Location
Alias           chdir                                            Set-Location
...

Now, how do I get the count of these objects?

解决方案

This will get you count:

get-alias | measure

You can work with the result as with object:

$m = get-alias | measure
$m.Count

And if you would like to have aliases in some variable also, you can use Tee-Object:

$m = get-alias | tee -Variable aliases | measure
$m.Count
$aliases

Some more info on Measure-Object cmdlet is on Technet.

Do not confuse it with Measure-Command cmdlet which is for time measuring. (again on Technet)

这篇关于如何在 PowerShell 中计算对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:19