Group membership

Roger Roger 6,406 Reputation points
2024-06-10T12:51:34.3+00:00

Hi All

The following script doesn't work for AD groups. Members of ADGroup1 should be able to add or remove users from other AD Group. please guide me.

# Define the $owner that will be able to manage the members of $group
 $owner = "ADGroup1";
 $group = "AnyADGroup";
     
 # Try to get objects from AD            
 try {
     
     $ownerobject = get-aduser $owner;
     $groupobject = get-adgroup $group;
     
 # If AD could not be read
 } catch {
     
     write-host "Could not get user/group information from Active Directory";
     break;
 }
     
 # Try to set "write members" rights on the group 
 try {
     $ldapstring = "LDAP://" + $groupobject.distinguishedname;
     $ldapgroup = [ADSI]$ldapstring;
     
     [System.DirectoryServices.DirectoryEntryConfiguration]$secoptions = $ldapgroup.get_Options();
     $secoptions.SecurityMasks = [System.DirectoryServices.SecurityMasks]'Dacl';
        
     # Get SID
     $identityref = $ownerobject.sid.value;
     $sid = new-object System.Security.Principal.SecurityIdentifier ($identityref);
     
     # Define rights to be applied
     $adrights = [System.DirectoryServices.ActiveDirectoryRights]::WriteProperty;
     $type = [System.Security.AccessControl.AccessControlType]::Allow;
     
     # Define permission attribute to modify (writeMembers)
     $objectguid = [Guid]"bf9679c0-0de6-11d0-a285-00aa003049e2";
     
     $adrule = new-object System.DirectoryServices.ActiveDirectoryAccessRule ($sid, $adrights, $type, $objectguid);
     
     # Apply new ACL
     $ldapgroup.get_ObjectSecurity().AddAccessRule($adrule);
     $ldapgroup.CommitChanges();
     
     write-host ("ACLs updated for group: " + $group);
     
     
 # If permissions could not be set
 } catch {
     
     write-host ("Could not set new ACLs on group: " + $group);
     break;
 }
Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,790 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,522 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,630 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,581 questions
{count} votes

Accepted answer
  1. MotoX80 34,346 Reputation points
    2024-06-10T15:02:20.1133333+00:00

    when i put $owner = "user1", it works fine

    If $owner contains a group name, then call get-adgroup and not get-aduser.

    $owner = "ADGroup1"; 
    $ownerobject = get-adgroup $owner;
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Roger Roger 6,406 Reputation points
    2024-06-10T14:26:52.1333333+00:00

    i am getting the below error for AD groups. The above scripts works for individual users for example when i put $owner = "user1", it works fine

    Could not get user/group information from Active Directory


  2. Roger Roger 6,406 Reputation points
    2024-06-10T15:55:30.32+00:00

    by make the below change it worked, i am unable to mark answer to this post.

    $owner = "ADGroup1";

    $ownerobject = get-adgroup $owner;


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.