gpt4 book ai didi

powershell - 在 PowerShell 中调用不带管道的 Sort-Object

转载 作者:行者123 更新时间:2023-12-03 11:25:43 25 4
gpt4 key购买 nike

我是 PowerShell 的新手,我只是想弄清楚它是如何工作的。

那么,我该如何编写这段代码:

Get-ChildItem C:\ | Sort-Object Length

多行代码?我试过这个:

$child_items = Get-ChildItem C:\
Sort-Object $child_items Length

但是没有用。我得到:

Sort-Object : A positional parameter cannot be found that accepts argument 'Length'.

最佳答案

尽管其他答案是使用命名参数传递值的正确方法,但请记住 get-help Sort-Object 说的内容:

When you use the InputObject parameter to submit a collection of items, 
Sort-Object receives one object that represents the collection.
Because one object cannot be sorted, Sort-Object returns the entire collection unchanged.

您会发现将 $child_items 传递给 -inputobject 不会完成任何排序操作。

您始终需要通过管道将值传递给 -inputobject

关于powershell - 在 PowerShell 中调用不带管道的 Sort-Object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15474243/

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