I create a PSCustom Object with some Properties and add this to a Collection
in short like
$Collection = @()
foreach ($item in $items)
{
$obj = New-Object PSCustomObject
$obj | Add-Member -NotePropertyName Property1 -NotePropertyValue ""
$obj | Add-Member -NotePropertyName Property2 -NotePropertyValue ""
$obj | Add-Member -NotePropertyName Property3 -NotePropertyValue ""
$Collection += $obj
}
that was ok so far and worked. Until i wanted to remove something from it. i got a message that the op_substraction is not a Method.
ok so i googled and found out that i could declare the Collection like this
$Collection = New-Object System.Collections.Generic.List[System.Object]
i change += to $Collection.Add($obj) now when i did $Collection.Remove($obj) i did not get an Error but the obj was not removed.
i googled more and found [System.Collections.ArrayList] first of all one more info.. i have following code to remove the object ($MyItem contains info which object should not be removed)
foreach ($Item in $Collection)
{
if ($MyItem -notcontains $Item.Value)
{
$Collection.Remove($Item)
}
}
so if i would do this it gives an error that $Collection was changed. ok so i clone the Opject List. I found some code on SO and changed it a bit
function clone-Collection($obj)
{
$newobj = New-Object System.Collections.Generic.List[System.Object]
foreach ($oobj in $obj)
{
$nobj = New-Object PsObject
$oobj.psobject.Properties | % { Add-Member -MemberType NoteProperty -InputObject $nobj -Name $_.Name -Value $_.Value }
$newobj.Add($nobj)
}
return $newobj
}
i call the Function and in the function everything is fine. But the ReturnValue now has 0,1,2,... at the beginning. i don't know why. i would like to suppress this.
further i read here that [System.Collections.ArrayList] is depricated.
so i'm pretty much lost. should i even use ArrayList if so how do i get rid of the numbers if i shouldn't use ArrayList what is the right alternativ. or am i doing something basic wrong?
please help me.
Thank you regards