TechnicalPowershell

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  graphitone  
 To:  ALL
42588.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. 

 
0/0
 Reply   Quote More 

Reply to All    
 

1

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2020 Project Beehive Forum

Forum Stats