We help IT Professionals succeed at work.

Combining two 'lists'

sirbounty
sirbounty asked
on
I'm running a script to gain some info from get-aduser and dumping to a bunch of CSVs.
I then collect each of these CSV files into one var, $ImportedData.
Next, I check the items present, because if there was a hiccup for whatever reason, I drop "ERROR" in the data, rather than the user's info.
So I filter $ImportedData | where {$_.displayname -like 'ERROR'}
I want to take that grouping and give it one more try, 90% of them work on a subsequent attempt, so I suspect launching these threads at once is causing a timeout.

So, I will be looping through the $Errors and running another get-aduser against the IDs.
But I then want to replace what's in $ImportedData with what I get from my second attempt, although not everything.
So if right now $ImportedData[46] has
ID         |   displayname         |     'Other Important Info'
123456|  ERROR                     |     'Your wife's birthday is 2/18!'

I want to ONLY replace ERROR with the display name (if it was successfully read).

So, how can I update only the ERROR fields in $ImportedData ?
Comment
Watch Question

Top Expert 2014

Commented:
I was doing something similar earlier today.
Basically you need to import everything, then for each object either pass back the object unchanged or modified.
$ImportedData = $ImportedData | ForEach `
{
    If ( $_.displayname -like 'ERROR' )
    {
        $line = $_
        try {
            Set-ADUser $line.samaccountname -DisplayName "something" -ErrorAction Stop
            $line.DisplayName = "updated"
            $line
        }
        catch {
            $line
        }
    }
    Else
    { $_ }
}

Open in new window

I'm not sure what operation you're putting in the try block, but I think you see the idea.
Top Expert 2007

Author

Commented:
Hey, great minds think alike, eh?  :^)

Figured I'd have to loop again, so there's no way to just loop through the 5 errors, instead of all 36,000 records, eh?
Top Expert 2014
Commented:
Actually, you can.  In my case, I was updating a field in a .CSV if an operation was successful.  I didn't really think about it if it's all in memory.  When it is, things are even simpler because you don't have to output the unchanged object.
$ImportedData | where {$_.displayname -like 'ERROR'} | % {
    #do something
    $_.displayname = "updated"
}

Open in new window

If you output $ImportedData you will see the elements have been updated.
Top Expert 2007

Author

Commented:
Makes perfect sense and that's what I was thinking of trying...just didn't think it would work, thus the post. :^)
Thanks sir
Top Expert 2014

Commented:
Isn't it nice when PS does most of the work for you?
Top Expert 2007

Author

Commented:
Haha - yeah.  And then EE helps with the leftovers. ;^)