排序对象不起作用

时间:2016-11-25 21:23:43

标签: powershell wmi

有谁知道为什么Sort-Object在此代码中不起作用?

gwmi win32_computersystem -comp $Computer.FullDomainName -ErrorAction SilentlyContinue |
    select Caption,Username | Sort Caption

1 个答案:

答案 0 :(得分:3)

我将扩展@Kage的评论并猜测答案。

gwmi win32_computersystem -comp $Computer.FullDomainName -ErrorAction SilentlyContinue |
select Caption,Username | Sort Caption

此查询获取单台计算机的标题,因此当您对其进行排序时,没有任何事情发生。 "laptop244" | sort什么也没做。我想你的代码看起来(大致)是这样的:

foreach ($Computer in (Get-AdComputer -Filter *))
{
  gwmi win32_computersystem -comp $Computer.FullDomainName -ErrorAction SilentlyContinue |
  select Caption,Username | Sort Caption
}

但它需要看起来像这样:

# Loop over the computers and gather all the results into one array

$ComputerDetails = foreach ($Computer in (Get-AdComputer -Filter *))
{
  gwmi win32_computersystem -comp $Computer.FullDomainName -ErrorAction SilentlyContinue |
  select Caption,Username
}

# Sort all the details in that array in one go
$ComputerDetails | Sort Caption