如何将 System.Collections.ArrayList 添加到 PowerShell 自定义对象?



我的目标是创建一个自定义数据对象,其中包含两个离散变量(fooNamefooUrl(和一个fooChildren列表,每个列表项有两个离散变量变量childAgechildName

目前,我有这个:

$fooCollection = [PSCustomObject] @{fooName=""; fooUrl=""; fooChildrenList=@()} 
$fooCollection.fooName = "foo-a-rama"
$fooCollection.fooUrl = "https://1.2.3.4"
$fooChild = New-Object -TypeName PSobject
$fooChild | Add-Member -Name childAge -MemberType NoteProperty -Value 6
$fooChild | Add-Member -Name childName -MemberType NoteProperty -Value "Betsy"
$fooCollection.fooChildrenList += $fooChild
$fooChild = New-Object -TypeName PSobject
$fooChild | Add-Member -Name childAge -MemberType NoteProperty -Value 10
$fooChild | Add-Member -Name childName -MemberType NoteProperty -Value "Rolf"
$fooCollection.fooChildrenList += $fooChild
cls
$fooCollection.fooName
$fooCollection.fooUrl
foreach ($fooChild in $fooCollection.fooChildrenList)
{
("  " + $fooChild.childName + " " + $fooChild.childAge)
}

这将产生以下内容。目前为止,一切都好

foo-a-rama
https://1.2.3.4
Betsy 6
Rolf 10

问题:我不喜欢使用+=,因为据我了解,每次执行+=时,使用+=会导致创建$fooCollection.fooChildrenList的副本(无论处于何种状态(。

因此,与其将fooChildrenList实现为@(),不如New-Object System.Collections.ArrayList实现fooChildrenList,以便我可以根据需要添加每一行。我已经尝试了各种在代码中执行此操作的方法,但最终fooChildrenList未填充。例如:

$fooCollection = [PSCustomObject] @{fooName=""; fooUrl=""; fooChildrenList = New-Object System.Collections.ArrayList} 
$fooCollection.fooName = "foo-a-rama"
$fooCollection.fooUrl = "https://1.2.3.4"
$fooChild.childName = "Betsy"
$fooChild.childAge = 6
$fooCollection.fooChildrenList.Add((New-Object PSObject -Property $fooChild))
$fooChild.childName = "Rolf"
$fooChild.childAge = 10
$fooCollection.fooChildrenList.Add((New-Object PSObject -Property $fooChild))

$fooCollection | get-member演出

TypeName: System.Management.Automation.PSCustomObject
Name            MemberType   Definition                                   
----            ----------   ----------                                   
Equals          Method       bool Equals(System.Object obj)               
GetHashCode     Method       int GetHashCode()                            
GetType         Method       type GetType()                               
ToString        Method       string ToString()                            
fooChildrenList NoteProperty System.Collections.ArrayList fooChildrenList=
fooName         NoteProperty string fooName=foo-a-rama                    
fooUrl          NoteProperty string fooUrl=https://1.2.3.4  

$fooCollection演出

fooName         : foo-a-rama
fooUrl          : https://1.2.3.4
fooChildrenList : {} 

如何将 System.Collections.ArrayList 添加到 PowerShell 自定义对象?

好吧,我不确定您遇到了什么问题,它对我来说效果很好

function New-Child([string]$Name, [int]$Age){
$Child = New-Object -TypeName PSobject
$Child | Add-Member -Name childAge -MemberType NoteProperty -Value $age -PassThru | 
Add-Member -Name childName -MemberType NoteProperty -Value $name
return $child
}
$fooCollection = [PSCustomObject] @{fooName=""; fooUrl=""; fooChildrenList = New-Object System.Collections.ArrayList} 
$fooCollection.fooName = "foo-a-rama"
$fooCollection.fooUrl = "https://1.2.3.4"
$fooCollection.fooChildrenList.Add((New-Child -Name "Betty" -Age 9)) | Out-Null
$fooCollection.fooChildrenList.Add((New-Child -Name "Ralf" -Age 15)) | Out-Null
$fooCollection.fooName
$fooCollection.fooUrl
foreach ($fooChild in $fooCollection.fooChildrenList)
{
"  " + $fooChild.childName + " " + $fooChild.childAge
}

输出

foo-a-rama
https://1.2.3.4
Betty 9
Ralf 15

挑战在于每次使用.Add()添加到列表中时,都要添加您重复使用的$fooChild[pscustomobject]实例的副本(如果您不使用副本,则最终所有列表元素都指向同一对象(。

但是,您无法使用New-Object PSObject -Property克隆现有[pscustomobject](也称为[psobject](实例

一种选择 (PSv3+( 是将可重用$fooChild定义为有序哈希表,然后使用[pscustomobject]强制转换每次都会隐式创建一个新对象:

$fooCollection = [PSCustomObject] @{ fooChildrenList = New-Object Collections.ArrayList } 
# Create the reusable $fooChild as an *ordered hashtable* (PSv3+)
$fooChild = [ordered] @{ childName = ''; childAge = -1 }
# Create 1st child and add to list with [pscustomobject] cast
$fooChild.childName = 'Betsy'; $fooChild.childAge = 6
$null = $fooCollection.fooChildrenList.Add([pscustomobject] $fooChild)
# Create and add another child.
$fooChild.childName = 'Rolf'; $fooChild.childAge = 10
$null = $fooCollection.fooChildrenList.Add([pscustomobject] $fooChild)
# Output the children
$fooCollection.fooChildrenList

请注意$null = ...,它抑制了.Add()方法调用中通常不需要的输出。

以上结果:

childName childAge
--------- --------
Betsy            6
Rolf            10

一个稍微晦涩的替代方法是坚持使用$fooChild作为[pscustomobject]实例并调用.psobject.Copy()来创建克隆。


ArcSet 的有用答案提供了一个更加模块化的解决方案,该解决方案通过帮助程序函数按需创建新的自定义对象实例


最后,在PSv5+中,您可以定义一个帮助程序类

$fooCollection = [PSCustomObject] @{ fooChildrenList = New-Object Collections.ArrayList } 
# Define helper class
class FooChild {
[string] $childName
[int]    $childAge
}
# Create 1st child and add to list with [pscustomobject] cast
$null = $fooCollection.fooChildrenList.Add([FooChild] @{ childName = 'Betsy'; childAge = 6 })
# Create and add another child.
$null = $fooCollection.fooChildrenList.Add([FooChild] @{ childName = 'Rolf'; childAge = 10 })
# Output the children
$fooCollection.fooChildrenList

请注意,如何通过简单地强制转换具有与类属性名称匹配的条目的哈希表来创建[FooChild]实例。

从我用来制作一些数组的东西中快速复制粘贴。我必须创建自定义对象,然后将它们添加到数组中。它需要根据您的场景进行修改,但我认为它会为您提供所需的内容。

[System.Collections.ArrayList]$SQL_Query_Results = @()
ForEach ($SQL_Index in $SQL_Table) {
$SQL_Array_Object = [PSCustomObject]@{
'Computer_Name'    = $SQL_Table[$SQL_Index_Counter].ComputerID -replace ",", ""
'Project'          = $SQL_Table[$SQL_Index_Counter].Project
'Site'             = $SQL_Table[$SQL_Index_Counter].Site
'Description'      = $SQL_Table[$SQL_Index_Counter].Description -replace ",", ""
'Physical_Machine' = $SQL_Table[$SQL_Index_Counter].IsPhysicalMachine
'Active'           = $SQL_Table[$SQL_Index_Counter].IsActive
}
$SQL_Query_Results.Add($SQL_Array_Object) | Out-Null
}

已编辑以显示最初如何创建数组。

最新更新