Help working with CSV files
i have csv file 3 columns
jobtitles-----adrole1----- adrole2
title1---------role1---------role4
title2---------role2---------role5
title2---------role3--------role6
and have list of active directory users in other variable $users.
i need ad users jobtitle match ones in first column of csv "jobtitles"
i like:
import-module activedirectory $csv2= import-csv e:\mycsv.csv | select-object jobtitles $jobtitles = @() foreach ($line in $csv2) { $jobtitles += $line.title } $users = foreach ($user in $result){ get-aduser $user.loginname -properties title, samaccountname } foreach ($user in $users) { if ($jobtitles -contains $user.title) { write-host $user.samaccountname $user.title } }
this works fine.
what don't know how reference same row in csv
for example if user jobtitle matches title1, add user ad group role1 , role4 (because in same row roles)
any idea?
thanks
in second line in script, " | select-object jobtitles" causes data jobtitles column in .csv file included in array of objects stored in $csv2 variable. if avoid doing that, of data .csv file present in $csv.
but don't think code shown "works fine". in first foreach statement, code references property called .title, whereas property in $csv appear .jobtitles. but, if had been coded correctly, wasted step, seems intended create array of "title" values, have in $csv2.
then second foreach processes data in undefined array called $result.
with these issues, little difficult determine happens in following code.
i'd suggest give try copy/paste complete code comment on.
al dunbar -- remember 'mark or propose answer' or 'vote helpful' appropriate.
Windows Server > Windows PowerShell
Comments
Post a Comment