Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Creating a steam of custom objects in a loop

Reply
 
Old 12-10-2006   #1 (permalink)
Mike Gale


 
 

Creating a steam of custom objects in a loop

I'm looking at creating a stream of custom objects, sorting them, then
processing further.

Select-Object gives a flavour of what I would like to do. However in this
case I want to create an object whose properties are not a subset of
predefined classes. I'll create the properties in code (in a loop) and emit
a collection of the objects.

Can anyone point me to an example for doing that?

My System SpecsSystem Spec
Old 12-11-2006   #2 (permalink)
Maximilian Hänel


 
 

Re: Creating a steam of custom objects in a loop

Hi Mike,

> Can anyone point me to an example for doing that?


Not sure if it's what you are looking for:

$array=@()
for($idx=0;$idx -lt 10;++$idx)
{
$o=1 | select Name, Lastname
$o.Name="Foo $idx";
$o.Lastname="Bar $idx";
$array+=$o;
}
$array| sort lastname -desc

cu

Max
My System SpecsSystem Spec
Old 12-11-2006   #3 (permalink)
Keith Hill [MVP]


 
 

Re: Creating a steam of custom objects in a loop


"Mike Gale" <MikeGale@discussions.microsoft.com> wrote in message
news:B2AF2386-306B-4CA2-989B-6196D8930523@microsoft.com...
> I'm looking at creating a stream of custom objects, sorting them, then
> processing further.
>
> Select-Object gives a flavour of what I would like to do. However in this
> case I want to create an object whose properties are not a subset of
> predefined classes. I'll create the properties in code (in a loop) and
> emit
> a collection of the objects.
>
> Can anyone point me to an example for doing that?


Here's some code I use in a script to create an object from a hashtable of
propname (key) / value (value) pairs:

if ($props.Count -gt 0) {
$statusObj = new-object system.management.automation.psobject
$statusObj.psobject.typenames[0] =
"Custom.Tfs.VersionControl.Status"
foreach ($key in $props.keys) {
add-member -input $statusObj -MemberType NoteProperty $key
$props["$key"]
}
Write-Output $statusObj
$props.Clear()
}

--
Keith


My System SpecsSystem Spec
Old 12-11-2006   #4 (permalink)
Mike Gale


 
 

RE: Creating a steam of custom objects in a loop

Thanks to Max and Keith both.

I adopted a PSObject style approach and used Add-Member.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
loop through all ad objects PowerShell
PowerShell Custom Objects PowerShell
Using entlib 2 from custom objects PowerShell
Creating custom objects with add-member PowerShell
Info: Can you create your own objects with custom properties. PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46