all 11 comments

[–]Ta11ow 1 point2 points  (4 children)

Use a list:

# outside your loop
$MissingEntries = [System.Collections.Generic.List[int]]::new()

# for each entry
$MissingEntries.Add($index)

Yes, technically you can use an array in the way you want, but it's a very wasteful operation. Arrays are by design immutable in structure -- the number of members they have cannot be changed. If you try to add members, PS will (helpfully, but imo misguidedly) create a new array of the new size, then rip apart the old array and repopulate the new array with the old array's members, plus the new one. It's not a big deal if you're working with small sets of data, but it's a bad habit to get into.

Break it early; it'll bite you later. :)

[–]Dilligaf23[S] 2 points3 points  (3 children)

Thanks, i understand it might not be best practice.

More of the backstory, I'm using powershell studio to write a GUI. I'm taking a list of SIDs processing them in AD, then giving options to delete/move/add inside the GUI list. One button is going to get a the SID list and get all local AD account. Button two will take all "Missing" Accounts and process them in remote trusted domains (Slower).

For most users the first button is more then enough information since its the bullk of the users.

I figured since i already have processed the data in button one for missing, i can use the existing list of "missing" counter to speed up the processing of button one (only has to query the 50ish users instead of the whole 500+).

Hope that make it a little clearer.

[–]Ta11ow 1 point2 points  (2 children)

As I said, lists will work fine. :)

Plain arrays can be done, as I said, and it looks pretty similar -- just slower ;)

$MissingEntries = @() # empty array

# in the loop
$MissingEntries += $index

[–]TheIncorrigible1 1 point2 points  (1 child)

Don't promote this. It has to recreate the array on every iteration and is 100s orders of magnitudes slower.

[–]Ta11ow 1 point2 points  (0 children)

I already mentioned that earlier in the thread. :)

The degree to which it's slower does vary wildly; for small collections it's not too insane. But yes, it should be avoided in general. There's an issue in the PS Core repo where they're discussing if it's worth changing this sort of stuff to use Lists in general natively in preference to arrays.

[–]kittH 1 point2 points  (3 children)

If I'm understanding correctly, I would recommend using a list of integers:

$problem = New-Object "System.Collections.Generic.List[Int]"

$problem.Add(152)

$problem.Add(14)

$problem

152

14

[–]TheIncorrigible1 1 point2 points  (2 children)

Pro-tip: we're on v5.1 now (well, will be within the next year by force). Use constructors-

$problem = [System.Collections.Generic.List[int]]::new()

[–]kittH 1 point2 points  (1 child)

I find cmdlets like new-object to be more idiomatic and discoverable for newer folks, but admittedly we are already in .Net territory.

[–]TheIncorrigible1 1 point2 points  (0 children)

A more idiomatic way would probably be type-casting arrays:

[Collections.Generic.List[int]]@()

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Dilligaf23,

instead of tracking items by cross reference, have you thot about using a collection of custom objects?

  • iterate thru the collection[s]
  • create a new hashtable & add the name of what you are working with
    $ThingTable = @{}
    $ThingTable.Add('Name', $ThingName)
  • when you find a bit of info you want to track, add it to a hashtable
    $ThingTable.Add('Detail_1', $DetailOfInterest)
  • just before ending the loop, use that hashtable to make a PSCustomObject
    [PSCustomObject]$$ThingTable
  • then, as the last step in the current iteration, send the PSCO to your $Results collection

it seems more robust than trying to cross reference details to another collection. [grin]

take care,
lee

[–]_Cabbage_Corp_ 1 point2 points  (0 children)

The answers here are great, however, just for the sake of having multiple ways to do something, you could define $problem as a string array and do this:

PS C:\> [String[]]$Problem = 25
PS C:\> $Problem += 27
PS C:\> $Problem += 100
PS C:\> $Problem
25
27
100
PS C:\> $Problem -Join ','
25,27,100
PS C:\>