我的目标是创建一个自定义数据对象,它有两个离散变量(fooName
和fooUrl
)和一个fooChildren
列表,每个列表项有两个离散变量变量childAge
和childName
。
目前,我有这个:
$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
实现为@()
,而是将fooChildrenList
实现为New-Object System.Collections.ArrayList
,因此我可以根据需要添加每一行。我已经尝试了各种方法在代码中这样做,但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
挑战是添加你每次使用$fooChild
添加到列表时重复使用的[pscustomobject]
.Add()
实例的副本(如果你不使用副本,你最终会得到指向同一列表元素的所有列表元素宾语)。
但是,您无法使用[pscustomobject]
克隆现有的[psobject]
(a.k.a New-Object PSObject -Property
)实例。
一个选项(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's helpful answer提供了一个更加模块化的解决方案,可以通过辅助函数按需创建新的自定义对象实例。
最后,在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]
的实例。
从我用来制作一些数组的东西快速复制粘贴。我必须创建自定义对象,然后将它们添加到Array。它需要针对您的场景进行修改,但我认为它可以满足您的需求。
[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
}
编辑以显示最初如何创建Array。