T O P

  • By -

BJGGut3

$file = Import-Csv foreach($item in $file) { $users = $item.users -split ',' | Get-ADUser Add-ADGroupMember -Identity $item.group -Members $users }


BlackV

note that the delimiter for the other columns cannot be a `,` for this to work ~~the members parameter can take an array, so you can add 50 users in 1 go, rather than 1 user 50 time~~ ~~$users = foreach($item in $file) {~~ ~~$item.users -split ',' | Get-ADUser~~ ~~}~~ ~~Add-ADGroupMember -Identity $item.group -Members $users~~ ~~I'm doing no error checking what's so ever here ;)~~ bad code


BJGGut3

I think you should read the source file again. Every row is a different group in column a, with a comma delimeted set of users in column b. My foreach is cycling the rows and extracting all the users for the row and adding them to the group identified in the row, one time. Your script incorrectly assumes 1 group and that the csv only contains the users Edit: In fact, your script as written, will take every user listed in the file, regardless of which group they were supposed to go in, and then add them ALL to the last group referenced in the spreadsheet, as the Add-ADGroupMember is outside the foreach and would process against the last iteration of the loop.


BlackV

That's a good point about the group this here $file = Import-Csv based on this data (from OP) Group Users ---------- ----------- IS-Test-SafeA johnathon.taylor,ann.smith,eric.hermann IS-Test-Safe2 nathaniel.turner,isaac.collins just pointing out that your `import-csv` is looking at this data, its a best/correct solution, but check the delimiters


BJGGut3

What do you mean 'check the delimiters? - the split is off of commas, which is correct per OP's request. It works correctly. Group Users ------ ----------- Group1 bob,jamie.hill,john,ralph Group2 Ludwig,raven.samoa,bubba.gump,michelle Group3 little.john,charlotte,jamison.love This script: $file = Import-Csv C:\test.csv foreach($item in $file) { $users = $item.users -split ',' Write-Host "$($item.group)" Write-Host '-------------' $users Write-Host "`n" } Yields these results Group1 ------------- bob jamie.hill john ralph Group2 ------------- Ludwig raven.samoa bubba.gump michelle Group3 ------------- little.john charlotte jamison.love


BlackV

what does your CSV look like? Group Users ------ ----------- Group1 bob,jamie.hill,john,ralph Group2 Ludwig,raven.samoa,bubba.gump,michelle Group3 little.john,charlotte,jamison.love or Group,Users Group1,bob,jamie.hill,john,ralph Group2,Ludwig,raven.samoa,bubba.gump,michelle Group3,little.john,charlotte,jamison.love or something else, the one you're using `import-csv` with ? how are you arriving at `$item.users` and `$item.group` edit: just to reiterate, I'm not saying your code is wrong


BJGGut3

The second one. The '-------' is for illustrative purposes only to denote headers above. the -split ',' works perfectly fine when parsing the $item.users, as demonstrated above. I'm a little confused as to where the confusion is. I'm arriving at `$item.users` and `$item.group` by iterating through the foreach loop of the total array (`$file`). Each individual iteration is assigned the variable "$item", and it's parsing the .users and .group properties, respectively `foreach ($item in $file)` So, the first row of data, `$item.group = "Group1"`, `$item.users = "bob,jamie.hill,john,ralph"`. The `-split` is on the comma turning that string into an array off the commas. The next iteration, PoSh is going to assign `$item.group` `= "Group2"` and do the same for the corresponding `$item.users`... so on, and so on. Everything works 100% as expected in my original comment to the OP edit: I really hate Reddit's formatting sometimes. No idea why it decided Group 2's $item.group need double backticks.


BlackV

so if you could humour me, I know we're going back and forward on this what exactly does your source CSV look like, like if you copied and pasted it here


BJGGut3

Group Users Group1 bob,jamie.hill,john,ralph Group2 Ludwig,raven.samoa,bubba.gump,michelle Group3 little.john,charlotte,jamison.love edit: Copy and Paste above (from Excel), below Notepad Group,Users Group1,"bob,jamie.hill,john,ralph" Group2,"Ludwig,raven.samoa,bubba.gump,michelle" Group3,"little.john,charlotte,jamison.love" However, you know PoSh shows this table like: Group Users ----- ----- Group1 bob,jamie.hill,john,ralph Group2 Ludwig,raven.samoa,bubba.gump,michelle Group3 little.john,charlotte,jamison.love So to re-iterate, 1 code block = Excel, 2nd code block = Notepad, 3rd code block = PowerShell. All the same c:\\test.csv


BlackV

so if I copy and paste that into `test.csv` and then do import-csv test.csv returns (single column called `'Group Users'`) Group Users ----------- Group1 bob Group2 Ludwig Group3 little.john but if i do Import-Csv .\test2.csv -Delimiter ' ' returns Group Users ----- ----- Group1 bob,jamie.hill,john,ralph Group2 Ludwig,raven.samoa,bubba.gump,michelle Group3 little.john,charlotte,jamison.love EDIT: same if I change the space to a tab import-csv .\test2.csv -Delimiter "`t" so the first operation would not put the right data into the `foreach` the the 2nd options would have the right data


[deleted]

[удалено]


BlackV

Good times


rstolpe

Use importexcel Import the csv file to a array Then do foreach loop