Syntax correction

Glenn Maxwell 12,456 Reputation points
2023-02-21T11:33:07.64+00:00

Hi All

I want to provide few users write members access to the AD group(i.e these users can add/remove users to the AD group). in the below syntax if i add one user it is working, if i add more than one user it is not working please guide me.

 $owner = "user1","user2","user3",user4";
 $group = "group1";          
 try {
     $ownerobject = get-aduser $owner;
     $groupobject = get-adgroup $group;
 } catch { 
     write-host "Could not get user/group information from Active Directory";
     break;
 }
 try {
     $ldapstring = "LDAP://" + $groupobject.distinguishedname;
     $ldapgroup = [ADSI]$ldapstring;
     [System.DirectoryServices.DirectoryEntryConfiguration]$secoptions = $ldapgroup.get_Options();
     $secoptions.SecurityMasks = [System.DirectoryServices.SecurityMasks]'Dacl';
     $identityref = $ownerobject.sid.value;
     $sid = new-object System.Security.Principal.SecurityIdentifier ($identityref);
     $adrights = [System.DirectoryServices.ActiveDirectoryRights]::WriteProperty;
     $type = [System.Security.AccessControl.AccessControlType]::Allow;
     $objectguid = [Guid]"bf9679c0-0de6-11d0-a285-00aa003049e2";
     $adrule = new-object System.DirectoryServices.ActiveDirectoryAccessRule ($sid, $adrights, $type, $objectguid);
     $ldapgroup.get_ObjectSecurity().AddAccessRule($adrule);
     $ldapgroup.CommitChanges();
     write-host ("ACLs updated for group: " + $group);
 } 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,970 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,599 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,912 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,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,686 Reputation points
    2023-02-21T16:21:36.0766667+00:00

    You've turned $owner into a list (it was a scalar before, with only one user as its value). You need to process each owner individually. BTW, the last item in the list is missing a double quote in your code.

    Try this:

    $owners = "user1", "user2", "user3", "user4"
    $group = "group1";
    foreach ($owner in $owners){
        try {
            $ownerobject = Get-ADUser $owner -ErrorAction STOP
            $groupobject = Get-ADGroup $group -ErrorAction STOP
        }
        catch { 
            Write-Host "Could not get user/group information from Active Directory";
            break;
        }
        try {
            $ldapstring = "LDAP://" + $groupobject.distinguishedname;
            $ldapgroup = [ADSI]$ldapstring;
            [System.DirectoryServices.DirectoryEntryConfiguration]$secoptions = $ldapgroup.get_Options();
            $secoptions.SecurityMasks = [System.DirectoryServices.SecurityMasks]'Dacl';
            $identityref = $ownerobject.sid.value;
            $sid = New-Object System.Security.Principal.SecurityIdentifier ($identityref);
            $adrights = [System.DirectoryServices.ActiveDirectoryRights]::WriteProperty;
            $type = [System.Security.AccessControl.AccessControlType]::Allow;
            $objectguid = [Guid]"bf9679c0-0de6-11d0-a285-00aa003049e2";
            $adrule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($sid, $adrights, $type, $objectguid);
            $ldapgroup.get_ObjectSecurity().AddAccessRule($adrule);
            $ldapgroup.CommitChanges();
            Write-Host ("ACLs updated for group: " + $group);
        }
        catch {
            Write-Host ("Could not set new ACLs on group: " + $group);
            break;
        }
    }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

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.