Pages

3/26/2011

Powershell: Add members to AD group


$users = get-content add.txt
$target = "testGroup"
"=============================="
" ADD TO " + $target
"------------------------------"
 $ds = new-object directoryServices.directorySearcher 
 $ds.filter = "(&(objectClass=Group)(name=$target))" 
 $dn = $ds.findOne() 

 if ($dn) { #found
    $group = [ADSI]$dn.path 
    $groupDE = [ADSI]"LDAP://$($group.distinguishedname)" 

    foreach ($name in $users)
     {
     $ds.filter = "(&(objectCategory=computer)(objectClass=user)(name=$name))" 
     $dn = $ds.findOne()
     if ($dn) {
     $usr = [ADSI]$dn.path
     $ADuser = [ADSI]"LDAP://$($usr.distinguishedname)"
     "    "+$ADuser.name
      $groupDE.add("LDAP://$($ADuser.distinguishedName)")  
              }#if
        }#foreach
    }#if
"------------------------------"

No comments: