开发者

Powershell add user to group

开发者 https://www.devze.com 2023-04-05 22:01 出处:网络
I am trying to read an XML file with user information and based on that information I want to add users to Active Directory gr开发者_StackOverflowoups. I have been looking up the error messages with n

I am trying to read an XML file with user information and based on that information I want to add users to Active Directory gr开发者_StackOverflowoups. 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)"


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.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消