79739162

Date: 2025-08-18 19:31:55
Score: 0.5
Natty:
Report link

I think you're mixing up a foreach-loop and a while-loop. What you actually want to do is take every line of content3, compare its name property to the name property in content2 and set the roles property of content3, if they match. You are iterating through the items in content3 with the foreach-loop, and then you re-iterate through them with the while loop. $j -lt $content3.length should be $j -lt $content2.length. You also have to set number of the line in content2 you are interested in, so $content2.Name should be $content2[$j].Name.

Moreover, set the delimiter for the csv files to ";" to be sure they're parsed correctly.

Finally - I'm not quite sure - but I think you should create the Custom Object first and then add it to content3.

I've tried the following, it produces the wanted output, I think:

# Import CSV files
$content1 = Import-Csv -Path .\test1.csv -delimiter ";"
$content2 =  Import-Csv -Path .\test2.csv -delimiter ";"

# Print existing CSV arrays
# $content1
# $content2

#create new array
$content3 =@()

# Populate new array with initial information and values
$i = 0
while ($i -lt $content1.count) {
    $obj = [PSCustomObject]@{UserName = $content1[$i].username; Name = $content1[$i].realName; Roles = ""}
    $content3 += $obj
    $i++
}

# Look for matching name from $content3 in $content2, and then populate the corresponding Groups column in $content3 with the corresponding ApprovedGroups in $content2
# Does not currently work
$j = 0
foreach ($row in $content3)  {
    while ($j -lt $content2.count) {
        if ($row.Name -match $content2[$j].Name) {$row.Roles = $content2[$j].ApprovedGroups ; break}
        $j++
    } 
}

# Print new array thus far
$content3 

Greetings

Johann

Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Johann