locked
Better way to write this script? RRS feed

  • Question

  • I have 2 csv files.  Groups.csv contains groups and Branch.csv contains the users I want in each group

    Groups.csv

    UG

    TeamA

    TeamB

    TeamC

    etc

    Branch.csv

    UPN

    UserA

    UserB

    UserC

    etc

    $groups = Import-Csv C:\Temp\Users\groups.csv 
    foreach ($group in $groups)
    {
        $UniGroups = $group.UG
        $Users = Import-Csv C:\Temp\Users\branch.csv
    
        foreach ($user in $users)
        {
            $linkname = $user.UPN
    
            Add-UnifiedGroupLinks -identity $unigroups -linktype members -links $linkname
        }
        
        
        
    }

    This does work, but seems to take a while just to add 8 users to 15 groups.

    Is there a better way to do this?  I tried 2 columns in a single csv but it was only adding 1 user per group that on the same row.

    Tuesday, May 15, 2018 3:10 PM

Answers

  • Try it like this, if works as expected remove the -WhatIf switch

    $users = Import-Csv C:\Temp\Users\branch.csv
    Import-Csv C:\Temp\Users\groups.csv | Foreach {
      Add-UnifiedGroupLinks -identity $_.UG -linktype members -links $users -WhatIf
    }
    Please also beware that the Links parameter takes the names in the format of

    • Name

    • Display name

    • Alias

    • Distinguished name (DN)

    • Canonical DN

    • Email address

    • GUID



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Tuesday, May 15, 2018 4:49 PM edit
    • Marked as answer by Chris.Greeson Friday, June 1, 2018 12:26 PM
    Tuesday, May 15, 2018 4:02 PM

All replies

  • You don't have to add them one by one. If you carefully review the help for Add-UnifiedGroupLinks you'll see the parameter -Links takes an array of IDs.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, May 15, 2018 3:25 PM
  • You're importing the branch.csv in every loop when you don't need to.

    import both the CSVs and then perform the foreach loops. Hopefully, that makes sense. Let me know if not.


    Learn PowerShell                     Script Requests

    -Remember to mark the correct response as the answer-

    Tuesday, May 15, 2018 3:26 PM
  • Try it like this, if works as expected remove the -WhatIf switch

    $users = Import-Csv C:\Temp\Users\branch.csv
    Import-Csv C:\Temp\Users\groups.csv | Foreach {
      Add-UnifiedGroupLinks -identity $_.UG -linktype members -links $users -WhatIf
    }
    Please also beware that the Links parameter takes the names in the format of

    • Name

    • Display name

    • Alias

    • Distinguished name (DN)

    • Canonical DN

    • Email address

    • GUID



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Tuesday, May 15, 2018 4:49 PM edit
    • Marked as answer by Chris.Greeson Friday, June 1, 2018 12:26 PM
    Tuesday, May 15, 2018 4:02 PM
  • I believe the Add-UnifiedGroupLinks cmdlet is for O365. But the Add-ADGroupMember cmdlet can add an array of users to a group. See:

    https://docs.microsoft.com/en-us/powershell/module/addsadministration/add-adgroupmember?view=win10-ps

    Use the -Members parameter of the cmdlet.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, May 15, 2018 5:19 PM
  • That works! Thanks!
    Friday, June 1, 2018 12:26 PM