Powershell

From: graphitone24 Mar 2020 00:46
To: ALL1 of 1
Would anyone be able to help out with a little powershell I'm having trouble sorting? 

I want to import a list of users into an AD group (group_a) from a csv file (a simple two column affair, showing display name and samaccountname) but want to exclude them if they're already a member of another group (group_b). So, I've got this to do the import:
 
Code: 
 Import-csv "filename.csv" | %{ add-adgroupmember "group_a" -member $_.samaccountname }
After some searching I found that it's possible to exclude based on something like this:
 
Code: 
    if ($SamID -in $group_b) {
        write-output "Skipping $SamID"
        continue
        } #End if ($SamID -in $group_b)
    
    if ($SamID -notin $group_a) {
        write-output "Adding $SamID to $group_a"
        } #End if ($SamID -notin $group_a)

   
I'm not sure how to shoe horn that second bit into the first. I imagine there's a 'for each' that needs to be in there, but I'm not very clear on the syntax required. Also, I'm not sure about the logic in the last line - end if the user account is not in group_a - I'm not sure that needs to be there at all. 

 
EDITED: 24 Mar 2020 00:47 by GRAPHITONE