I have a CSV containing 300K+ records. Using this as a base, I need to extract the value from Login column, feed it in get-aduser command and extract reporting manager and one other custom property for each user. the 2 additional values need to be appended to a copy of this large CSV
Unfortunately I have had no luck optimising
I tried the hashtable approach
Since Login column of CSV has multiple rows containing same userid(each row corresponds to the different DB roles the user has), I extracted a list of unique userids. Out of 300K records, I got some 1700-1800 unique userids
Then I created a hashtable
$f1 = import-csv -path $CSVFilePath/$file -Delimiter '|' -Header Login,Name,Role,System,Permission
$h2 = @{} $f1 | ForEach-Object { $h2[$_.Login] = $_ }
- I tried updating the hashtable with the custom properties
$f1 |Foreach-object { $Login=$_.Login $Array | foreach-object{ if($h2.ContainsKey( $_.userId)){ $h2[$Login] | Add-Member -Name "REPORTING_MANAGER" -Value $_.Manager -MemberType NoteProperty -Force $h2[$Login] | Add-Member -Name "CUSTOM_COLUMN" -Value $_.CustomProp-MemberType NoteProperty -Force } } }
But the last loop is taking considerable time. Not sure how this can be made faster. Help !!
EDIT
Here are some details I forgot to include
$uniqueLogin = import-csv -path $CSVFilePath/$file -Delimiter '|' | Sort Login -Unique | Select Login
$uniqueUsers = $uniqueLogin | Select-Object -Skip 1
$uniqueUsers.Count
$Array = @()
$uniqueUsers | % {
$userid = $_."Login"
$userProperties = get-aduser $userid -Server xxx -Properties * | select Manager,extensionAttribute7
if($userProperties.Manager -ne $null){
$manager = ($userProperties.Manager.Split(',')[0] + $userProperties.Manager.Split(',')[1]).Replace('\', ',').Replace('CN=', '')
}
if($userProperties.extensionAttribute7 -ne $null){
$customprop=$userProperties.extensionAttribute7
}
$ResultObject = [PSCustomObject] @{ #Return Object
userId = $userid
Manager = $manager
Customprop= $customprop
}
$Array += $ResultObject
}
so basically $Array is an object of this kind
[
{
"userId": "xxxx",
"Manager": "yyyy",
"CustomProp": "test1"
},
{
"userId": "yyyyyy",
"Manager": "zzzzz",
"Customprop": "test2"
}
]
SAMPLE SOURCE CSV