Powershell Active Directory Users to Multiple Groups using CSV -
i'm trying assign groups users in active directory. in simple case, case doing loop through csv file. however, in circumstances, csv different. example of csv followed.
tjone,time,jones,time jones,tim.jones@home.ac.uk,time jones,home work, manager, finance, staff, "ou=groups,dc=home,dc=ac,dc=uk",finance;hr,p@ssw0rd
the script followed:
#read csv file $pathofcsv = "c:\users\administrator\desktop\users.csv" $header = "samaccountname","givenname","sn","displayname","mail","cn","company","title","department","orgunit","ou","groups","password" $infocsv = import-csv $pathofcsv -delimiter "," -header $header foreach ($user in $infocsv) { add-adgroupmember -identity $user.groups -members $user.samaccountname }
this different can see i'm trying process groups column has more 1 name , make matter worse, separated ";". when run script, see 2 name one.
question is, possible separate 2 names , processing them i.e. assigning 2 or more group names 1 column.
i'm in unfortunate position can't change csv have received.
any sort of world appreciated.
as has been pointed out in comments, use -split
operator:
foreach($user in $infocsv) { foreach($group in $user.groups -split ';') { add-adgroupmember -identity $group -members $user.samaccountname } }
if powershell 2.0 (-split
introduced in version 3.0), can use string.split()
method:
foreach($user in $infocsv) { foreach($group in $user.groups.split(';')) { add-adgroupmember -identity $group -members $user.samaccountname } }
Comments
Post a Comment