Q:

How to map column to another column value from csv file? 

Suppose i have a csv file as follows:

role    member
role1   memberA
role1   memberB
role2   memberC
role3   memberX
role3   memberY
role3   memberC

I want to add to a database all members to the roles on the database
$csvR = (Import-Csv $csvFile).role

        foreach ($Role in $csvR)
        {
            foreach ($Member in $Role) 
            {   
                & ".\AddMembers.ps1" -AddtoDatabaseOnOneServer "$Role" "$Member" "$DatabaseName" "$ServerInput"
            } 
        }

Will this loop recognize for example to add memberA to role1, memberB to role1, memberC to role2, etc.?  in other words, is this inner loop correct: foreach ($Member in $Role) 

A:

here's a demo of how to use the Group-Object cmdlet to iterate thru the items in an imported CSV file by a grouped property. [ grin]     
at the give person line, you can either add your call to your script for each person OR gather the names of all the persons for that .Role and add them all at once.     
# fake reading in a CSV file
#    in real life, use Import-CSV
$InStuff = @'
role, member
role1, memberA
role1, memberB
role2, memberC
role3, memberX
role3, memberY
role3, memberC
'@ | ConvertFrom-Csv

$GroupedIS = $InStuff |
    Group-Object -Property Role

foreach ($GIS_Item in $GroupedIS)
    {
    foreach ($GG_Item in $GIS_Item. Group)
        {
        'give person [ {0} ] the role [ {1} ]' -f $GG_Item. Member, $GG_Item. Role
        }
    }

output ...   
give person [ memberA ] the role [ role1 ]
give person [ memberB ] the role [ role1 ]
give person [ memberC ] the role [ role2 ]
give person [ memberX ] the role [ role3 ]
give person [ memberY ] the role [ role3 ]
give person [ memberC ] the role [ role3 ]

