Powershell add user to group

1

I am trying to read an XML file with user information and based on that information I want to add users to Active Directory groups. I have been looking up the error messages with no help so far. Here is the add user to group code:

 $MyUsers = [xml] (Get-Content e:\sample.xml)
 $a = 0
 $b = 0
 $c = 0
 $OUServer = "servername.domain.domain.edu"
 $AD3Server = "servername.domain.domain.edu"
 $DSSPath = "cn=Atl-Users,OU=HR,DC=domain,DC=domain,DC=edu"
 $AD3Path = "ou=Admin,DC=domain,DC=domain,DC=domain,DC=EDU"
 $connection = "LDAP://$OUServer/$DSSPath"
     LOOP LOGIC
     {
     $CurUser = $MyUsers.clusters.cluster[$a].departments.department[$b].people.person[$c].loginid
     $Group = [adsi]"$connection"
     $User = "LDAP://$AD3Server/$CurUser,$AD3Path"
     $Group.Add($User) 
     }

Here is the error I get

Exception calling Add with 1 argument(s): "The server is unwilling to process the request. (Exception from HRESULT: 0x80072035)"

powershell
active-directory
asked on Stack Overflow Sep 18, 2011 by Scott • edited Aug 30, 2018 by Hexaholic

2 Answers

2

This do what you need:

$Connection = "LDAP://Server/CN=MyGoup,OU=MyOU,DC=MY,DC=CORP"

$Group = [adsi] $Connection

$User = "LDAP://Server/CN=MyUser,OU=MyOU,DC=MY,DC=CORP"

$Group.Add($User)

You have to check the contents of $CurUser AND $User variables.

answered on Stack Overflow Sep 19, 2011 by CB.
2

Here is a working example, you perhaps can adapt it.

First you forget to call the setinfo(), which is a kind of commit.

Second be careful that the value of $CurUser is in the form of 'CN=XXXXX'.

Clear-Host

# Connecting without User/Password to Active Directory
#$dn = [adsi] "LDAP://192.168.30.200:389/dc=dom,dc=fr"
# Connecting with User/Password to Active Directory
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.234.200:389/dc=dom,dc=fr","administrateur@dom.fr","admin")

# Creation of an OU
$Monou = $dn.create("OrganizationalUnit", "ou=Monou")
$Monou.put("Description", "Une description")
$Res = $Monou.Setinfo()

# Basic creation of a user
$objUtilisateur = $Monou.create("inetOrgPerson", "cn=Marc Assin")
$objUtilisateur.setinfo()

$objUtilisateur.samaccountname = "Massin"
$objUtilisateur.givenName = "Marc"
$objUtilisateur.sn = "Assin"
$objUtilisateur.userPrincipalName = "Massin@dom.fr"
# Set the state of the account
$objUtilisateur.pwdLastSet = 0
$objUtilisateur.userAccountControl = 544 #512
$objUtilisateur.SetInfo()

# Creation of a group
$MonGroupe = $Monou.Create("Group", "cn=MonGroupe")
$ADS_GROUP_TYPE_GLOBAL_GROUP = 0x00000002
$ADS_GROUP_TYPE_SECURITY_ENABLED = 0x80000000
$groupeType = $ADS_GROUP_TYPE_SECURITY_ENABLED -bor $ADS_GROUP_TYPE_GLOBAL_GROUP

$MonGroupe.put("groupType",$groupeType) 
$MonGroupe.setinfo()

# Adding user to a group
$MonGroupe.add('LDAP://cn=Marc Assin,ou=Monou,dc=dom,dc=fr')
$MonGroupe.setinfo()
answered on Stack Overflow Sep 19, 2011 by JPBlanc

User contributions licensed under CC BY-SA 3.0