powershell where-object将psobject的类型更改为pscustomobject -如何解决?

n6lpvg4x  于 2023-06-29  发布在  Shell
关注(0)|答案(1)|浏览(146)

示例代码:

$test=@()       
$new = New-Object PSObject
$new | Add-Member -type NoteProperty -name name -Value 'test'
$new | Add-Member -type NoteProperty -name nummer -Value "17580-10"
$new | Add-Member -type NoteProperty -name datum -Value "10.08.23"
$test += $new
$test

$test_filtered=@()
$test_filtered=$test | Where-Object {($_.nummer -match '175')}

输出:

PS C:\Users\cm> $test.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     True     Object[]                                 System.Array                                                                                                                       

PS C:\Users\cm> $test_filtered.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     False    PSCustomObject                           System.Object

如你所见,类型变了。我需要保持对象(之前)的原样。如何解决这个问题?
谢谢你的主意。

gcuhipw9

gcuhipw91#

您正在用psobject覆盖Array对象(Where-Object返回单个项目),如果您希望保留Array类型,请将筛选后的数据结果添加到数组中。所以更改这一行:

$test_filtered = $test | Where-Object {($_.nummer -match '175')}

致:

$test_filtered += ($test | Where-Object {($_.nummer -match '175')})

PS C:\> $test.GetType()
    
IsPublic IsSerial Name                                     BaseType                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                  
True     True     Object[]                                 System.Array                                                                                                                              
   

PS C:\> $test_filtered.GetType()
    
IsPublic IsSerial Name                                     BaseType                                                                                                                                  
-------- -------- ----                                     --------                                                                                                                                  
True     True     Object[]                                 System.Array

相关问题