Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 使用-降序时,排序对象生成不同的对象_Powershell_Sorting - Fatal编程技术网

Powershell 使用-降序时,排序对象生成不同的对象

Powershell 使用-降序时,排序对象生成不同的对象,powershell,sorting,Powershell,Sorting,为什么使用降序时排序对象会产生不同的对象?NoteProperty成员不同 Get-Date | Select-Object -Property BaseType,Name | Sort-Object -Property BaseType, Name | Get-Member TypeName: Selected.System.DateTime Name MemberType Definition ----

为什么使用降序时排序对象会产生不同的对象?
NoteProperty
成员不同

Get-Date | 
Select-Object -Property BaseType,Name | 
Sort-Object -Property BaseType, Name  | 
Get-Member

  TypeName: Selected.System.DateTime

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
BaseType    NoteProperty object BaseType=null          
Name        NoteProperty object Name=null              



Get-Date | Select-Object -Property BaseType,Name | 
Sort-Object -Property BaseType, Name -Descending | 
Get-Member

   TypeName: Selected.System.DateTime

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
BaseType    NoteProperty object BaseType=null          
Name        NoteProperty object Name=null              
此外,在写入控制台时,除非使用了
-降序
,否则不会显示
名称
属性。为什么呢

C:>Get-Type | Select-Object -Property BaseType,Name | gm

   TypeName: Selected.System.RuntimeType

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
BaseType    NoteProperty RuntimeType BaseType=System.Object
Name        NoteProperty string Name=Registry

C:>Get-Type | Select-Object -Property BaseType,Name | Sort-Object -Property BaseType,Name | gm

   TypeName: Selected.System.RuntimeType

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
BaseType    NoteProperty object BaseType=null
Name        NoteProperty string Name=_Activator

C:>Get-Type | Select-Object -Property BaseType,Name | Sort-Object -Property BaseType,Name -Descending | gm

   TypeName: Selected.System.RuntimeType

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
BaseType    NoteProperty RuntimeType BaseType=System.Xml.Xsl.XsltException
Name        NoteProperty string Name=XsltCompileException

很抱歉没有包含有关
获取类型的信息

这不是一个本机属于PoSH的cmdlet。要么是OP写的,要么是从另一个来源得到的。如果来自OP以外的其他来源,则应联系该作者,询问预期结果

如果使用内置cmdlet执行相同的操作,例如Get-Date,我们会看到所有成员都是相同的

Get-Date | 
Select-Object -Property BaseType,Name | 
Sort-Object -Property BaseType, Name  | 
Get-Member

  TypeName: Selected.System.DateTime

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
BaseType    NoteProperty object BaseType=null          
Name        NoteProperty object Name=null              



Get-Date | Select-Object -Property BaseType,Name | 
Sort-Object -Property BaseType, Name -Descending | 
Get-Member

   TypeName: Selected.System.DateTime

Name        MemberType   Definition                    
----        ----------   ----------                    
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()             
GetType     Method       type GetType()                
ToString    Method       string ToString()             
BaseType    NoteProperty object BaseType=null          
Name        NoteProperty object Name=null              

因此,似乎特别指向OP正在寻找的此cmdlet的实现。

您的
GetType
调用可能会导致一个以
$null
开头的数组,因为它有一个try-catch块,提供了
$null
的错误值。然后,如果将任何参数传递给
获取类型
,则该null将被过滤掉,但您不传递任何参数,因此null仍然存在于输出中。然后,当结果通过管道传输到
选择对象
,只解析管道中的第一个对象,以检查是否存在所有要显示的列,如果null是第一个对象,则它没有属性,因此不会显示任何内容


要修复此问题,请将
| Where对象{$\ne$null}
添加到Get-Type.ps1脚本中最后一行的
| Where对象-FilterScript$WhereBlock
前面。这将过滤掉上一个try-catch块产生的任何空值,您将只得到一个具有值的对象数组。

有趣,但是您可以提供一个<代码>获取类型
在我的机器上不是命令,如果不知道实际发送到
选择对象
中的内容,很难判断发生了什么。不知道
Get Type
是什么。问题在于
Get Type
的来源。如果需要,我可以在问题中发布
Get Type
的源代码。它不是很大。我试着按照你的建议更改代码,但得到了相同的结果。我还尝试了
$\uu0.BaseType-ne$null
$\u0.BaseType-ne'
,但都得到了相同的结果。@lit有趣的是。也许可以尝试抓取
Get Type
的整个列表,并检查其中的所有对象是否都有name属性。如果排序在输出上产生各种结果,那么列表应该会有一些问题。