Group Membership Add-Remove based on .csv

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Bucky Koehler Bucky Koehler 2 years, 6 months ago.

  • Author
    Posts
  • #16221
    Profile photo of Tim Blough
    Tim Blough
    Participant

    I'm attempting to add Active Directory Users to Groups.

    I have a file with a list of classes and Login Names. (attached)

    Each line in the file contains a class name (class_name) and a Login Name amongst other fields.

    This seemed to work to add membership:

    Import-CSV c:\scripts\ClassinfoStuName.csv | Where-Object {$_.school_bldg_code -ne "TADU" -and $_.subject_id -ne "ACA"} |
    Foreach-object {
    	$classNameFiltered = $_.class_name.Replace("/","-").Replace(":","-")
    	$loginName = $_.loginName
    	Add-ADGroupMember -Identity $classNameFiltered -Members $loginName
    }
    

    Now, when a student is dropped from a class, how do I remove the student from the group assignment?
    My logic is failing me.

    Should I create some sort of table object that builds a more straight forward link? Such as:
    CLASSName: loginNames
    ——————– ——————
    English 9 Honors-3 "Hubbard.Mother","Obama.Michelle","Cheney.Dick"

    Then somehow state:

     If $_.loginName does not exist in LoginNames then Remove-ADGroupMember -Identity $ClassnameFiltered $_.loginName
    
  • #16225
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There are many ways to handle things. For instance, if only people in the spreadsheet are supposed to be in the group, you could just remove everyone and then run the code above. You can also do something like this, I did NOT test this, but basic logic should work. Let us know.

    #Grab only what we need from the CSV
    $csv = Import-CSV C:\Users\rsimmers.INT\desktop\ClassinfoStuName.csv |
           Where-Object {$_.school_bldg_code -ne "TADU" -and $_.subject_id -ne "ACA"} |
           Select LoginName, @{Name='ClassName';Expression={$_.class_name.Replace("/","-").Replace(":","-")}}
    
    #loginName          ClassName             
    #---------          ---------             
    #Year.Good          TOPICS IN BIO I - 9   
    #Balboa.Rocky       TOPICS IN BIO I - 9   
    #Pitt.Brad          TOPICS IN BIO I - 9   
    #StJames.Rebecca    MODIFIED ENGLISH - 1  
    #Obama.Barak        MODIFIED ENGLISH - 1  
    #Ark.Noah           PHYSICAL EDUCATION - 2
    #ecret.Victoria    PHYSICAL EDUCATION - 2
    #Jean.Billie        PHYSICAL EDUCATION - 2
    #mith.Jim          PHYSICAL EDUCATION - 2
    #linton.Chelsy     PHYSICAL EDUCATION - 2
    #Musk.Elon          PHYSICAL EDUCATION - .....
    
    #Group the CSV items by the group name and then loop through each grouped item (e.g. ClassName)
    
    #Count Name                      Group                                                                                       
    #----- ----                      -----                                                                                       
    #    3 TOPICS IN BIO I - 9       {@{loginName=Year.Good; ClassName=TOPICS IN BIO I - 9}, @{loginName=Balboa.Rocky; ClassNa...
    #    2 MODIFIED ENGLISH - 1      {@{loginName=StJames.Rebecca; ClassName=MODIFIED ENGLISH - 1}, @{loginName=Obama.Barak; C...
    #    5 PHYSICAL EDUCATION - 2    {@{loginName=Ark.Noah; ClassName=PHYSICAL EDUCATION - 2}, @{loginName=Secret.Victoria; Cl...
    #    8 PHYSICAL EDUCATION - 9    {@{loginName=Musk.Elon; ClassName=PHYSICAL EDUCATION - 9}, @{loginName=White.Jessica; Cla...
    #    2 DRAMA - 2                 {@{loginName=Rice.Condoleza; ClassName=DRAMA - 2}, @{loginName=Cheney.Dick; ClassName=DRA...
    #    1 FRENCH 2 - 4              {@{loginName=Bush.Barbara; ClassName=FRENCH 2 - 4}}                                         
    #    2 CP ALGEBRA II - 7         {@{loginName=Joe.Billie; ClassName=CP ALGEBRA II - 7}, @{loginName=State.Kent; ClassName=...
    #    6 CP ALG 1-PT.2 - 1         {@{loginName=Jean.Billie; ClassName=CP ALG 1-PT.2 - 1}, @{loginName=Reagan.Nancy; ClassNa...
    #    4 CP GEOMETRY - 6           {@{loginName=Green.Rodney; ClassName=CP GEOMETRY - 6}, @{loginName=Rice.Condoleza; ClassN...
    #    4 ENGLISH 9 HONORS - 3      {@{loginName=Joe.Billie; ClassName=ENGLISH 9 HONORS - 3}, @{loginName=Cheney.Dick; ClassN...
    
    
    $csv | Group-Object -Property ClassName | ForEach-Object{
        #Create\Emtpy an array for the members in the CSV...
        $NewMembers = @()
        #Foreach group item... 
        $_.Group | ForEach-Object{
            #Add the name to an array...
            $NewMembers += $_.LoginName
            #Get the ClassName we are working on...
            $groupName = $_.ClassName
            #Add to group (you will either have to put logic here to see if they are already a member or set the error level to SilentlyContinue)
            Add-ADGroupMember -Identity $groupName -Members $_.LoginName -WhatIf
        }
        
        #Now you have all the students in the CSV in your NewMember array,
        #next you need to compare it against the current group...
        #Grab the members of the group currently...
        $ClassGroupMembers = Get-ADGroupMember -Identity $groupName | Select -ExpandProperty SamAccountName
    
        foreach ($Member in $ClassGroupMembers) {
            #If the current member of the group is not in the array created from the CSV, use Remove-AdGroupMember
            if ($NewMembers -notcontains $Member) {
                Remove-AdGroupMember -Identity $groupName -Members $Member -WhatIf
            }
        }
    } 
    
  • #16226
    Profile photo of Bucky Koehler
    Bucky Koehler
    Participant

    Compare-Object

    Edited the code as Rob Simmers import CSV example was better, this is just a different way to do the foreach loop at the bottom of his code.

    $ad = Get-ADGroupMember $classNameFiltered
    $result = Compare-Object $csv.loginName $ad.samaccountname
    $remove = $result | ?{$_.SideIndicator -eq "=>"}
    $add = $result | ?{$_.SideIndicator -eq "< ="}
    
    foreach($loginName in $remove.inputobject){Remove-ADGroupMember -Identity $classNameFiltered -Members $loginName -whatif}
    foreach($loginName in $add.inputobject){Add-ADGroupMember -Identity $classNameFiltered -Members $loginName -whatif}
    

    Editor code formatting is changing the SideIndicator operators, thus double check those and change as needed.

You must be logged in to reply to this topic.