none
Add User to Security group base on Country Attribute RRS feed

  • Question

  • I have users in many countries and need an automated way to add these users to specific security groups base on the Country Attribute of each user object.

    I came across this script that is kinda what I am looking for but does not work:

    Get-ADUser -SearchBase ‘OU=Users,OU=City,DC=company1,DC=com’ | Get-ADUser -Properties co | Where-Object {$_.co -eq "united states"} | % {Add-ADGroupMember “SG-USUsers” $_.SamAccountName}

    If anyone can get this script to work or has a script that they would like to share that solves this issue, that would be great.

    Noe

    Tuesday, October 18, 2016 2:59 AM

Answers

  • Get-ADUser -SearchBase 'OU=Users,OU=City,DC=company1,DC=com' -Filter * -Properties CO|
    	ForEach-Object{
    		$group = switch ($_.Co){
    			'United States' {'SG-USUsers'}
    			'Canada' { 'SG-CanadaUsers' }
    			'Mexico' { 'SG-MexicoUsers' }
        	}
    		Add-ADGroupMember $group -Member $_
    	}


    \_(ツ)_/


    Tuesday, October 18, 2016 3:34 AM
  • made a small change to "add-groupmember" to "add-adgroupmember"  and it worked!!

    this is what worked for me:

    Get-ADUser -SearchBase 'OU=Users,OU=City,DC=company1,DC=com' -Filter * -Properties CO| ForEach-Object{ $group = switch ($_.Co){ 'United States' {'SG-USUsers'} 'Canada' { 'SG-CanadaUsers' } 'Mexico' { 'SG-MexicoUsers' } } Add-ADGroupMember $group -Member $_ }

    thanks so much for you help.  this is exactly what I am looking for.  now I can add more sites to this script. 

    Noe

    • Marked as answer by Noe.O Wednesday, October 26, 2016 1:30 PM
    Wednesday, October 19, 2016 4:56 PM

All replies

  • Get-ADUser -SearchBase 'OU=Users,OU=City,DC=company1,DC=com' -Filter * -Properties CO|
    	ForEach-Object{
    		$group = switch ($_.Co){
    			'United States' {'SG-USUsers'}
    			'Canada' { 'SG-CanadaUsers' }
    			'Mexico' { 'SG-MexicoUsers' }
        	}
    		Add-ADGroupMember $group -Member $_
    	}


    \_(ツ)_/


    Tuesday, October 18, 2016 3:34 AM
  • made a small change to "add-groupmember" to "add-adgroupmember"  and it worked!!

    this is what worked for me:

    Get-ADUser -SearchBase 'OU=Users,OU=City,DC=company1,DC=com' -Filter * -Properties CO| ForEach-Object{ $group = switch ($_.Co){ 'United States' {'SG-USUsers'} 'Canada' { 'SG-CanadaUsers' } 'Mexico' { 'SG-MexicoUsers' } } Add-ADGroupMember $group -Member $_ }

    thanks so much for you help.  this is exactly what I am looking for.  now I can add more sites to this script. 

    Noe

    • Marked as answer by Noe.O Wednesday, October 26, 2016 1:30 PM
    Wednesday, October 19, 2016 4:56 PM