Powershell使用Group-Object创建哈希表


支持所有PS版本

Group-Object能够将很多对象其中相同的属性一起归为一组。

尤其是使用Group-Object得到它们的哈希表这将是非常有用的。下面将把全部启动的服务放到一起存到哈希表中:

$hash = Get-Service | 
  Group-Object -Property Status -AsHashTable -AsString

你现在可以输出所有运行或停止的服务,请这样:

$hash.Running
$hash.Stopped

你还可以分组更多的属性。这个例子将文件分成3堆:一组为小、一组为中还有一组为大。

$code = 
{
  if ($_.Length -gt 1MB)
  {'huge'}
  elseif ($_.Length -gt 10KB)
  {'average'}
  else
  {'tiny'}
}

$hash = Get-ChildItem -Path c:\windows |
  Group-Object -Property $code -AsHashTable -AsString


#$hash.Tiny
$hash.Huge

原文地址:Use Group-Object to Create Hash Tables

本文链接: https://www.pstips.net/use-group-object-to-create-hash-tables.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注