【问题标题】:PowerShell: How to add 1 user to multiple Active Directory Security Groups - Security tab of the security group with write permissionPowerShell:如何将 1 个用户添加到多个 Active Directory 安全组 - 具有写入权限的安全组的安全选项卡
【发布时间】:2019-12-11 11:15:02
【问题描述】:

我正在尝试将 1 个 ID 添加到 Active Directory 中的多个安全组。 该ID只需要添加到安全组的“安全选项卡”中,不需要添加为成员。

我需要为此 ID 设置“写入”权限。

有没有办法在 Power-Shell 中做到这一点?

【问题讨论】:

    标签: powershell active-directory powershell-4.0 network-security-groups


    【解决方案1】:

    here 的说明,尽管这使用户可以完全控制组(包括删除权限),并且存在一些其他问题(例如硬编码的用户名)。

    我已经为您修改了该示例,只授予GenericWrite 权限,并接受用户名作为参数。这还假设您运行它的用户、组和计算机都在同一个域中:

    function Set-GroupSecurity {
    [CmdletBinding()]
    param (
     [string] $GroupName,
     [string] $UserName
    )
        $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
        $root = $dom.GetDirectoryEntry()
    
        $search = [System.DirectoryServices.DirectorySearcher]$root
        $search.Filter = "(&(objectclass=group)(sAMAccountName=$GroupName))"
        $search.SizeLimit = 3000
        $result = $search.FindOne()
    
        $object = $result.GetDirectoryEntry()
    
        $sec = $object.ObjectSecurity
    
        ## set the rights and control type
        $allow = [System.Security.AccessControl.AccessControlType]::Allow
        $read = [System.DirectoryServices.ActiveDirectoryRights]::GenericRead
        $write = [System.DirectoryServices.ActiveDirectoryRights]::GenericWrite
    
        ## who does this apply to
        $domname = ([ADSI]"").Name
        $who = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList "$domname", $UserName
    
        # apply rules
        $readrule = New-Object -TypeName System.DirectoryServices.ActiveDirectoryAccessRule -ArgumentList $who, $read, $allow
        $sec.AddAccessRule($readrule)
    
        $writerule = New-Object -TypeName System.DirectoryServices.ActiveDirectoryAccessRule -ArgumentList $who, $write, $allow
        $sec.AddAccessRule($writerule)
    
        # tell it that we're only changing the DACL and not the owner
        $object.get_Options().SecurityMasks = [System.DirectoryServices.SecurityMasks]::Dacl
    
        # save
        $object.CommitChanges()
    }
    

    您可以将其粘贴到 PowerShell 提示符中,然后按 Enter。这将使该功能可供使用。然后你可以这样使用它:

    Set-GroupSecurity -GroupName "TstGroup1" -UserName "someone"
    

    【讨论】:

    • 非常感谢您的帮助!谢谢!我只是试了一下,但我得到了以下错误。我正在学习 PS,但仍然不知道这个......使用“0”参数调用“CommitChanges”的异常:“发生约束违规。”在 line:30 char:5 + $object.CommitChanges() + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
    • 我找到了该问题的描述here。我更新了答案中的代码。现在我可以实际测试了,我也意识到读取权限也需要单独设置,所以我添加了。
    • 非常感谢!这是一个很大的帮助。希望善行早日回到你身边! :)
    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    相关资源
    最近更新 更多