首页 > 解决方案 > 如何在 AD 中的所有 OU 中创建安全组

问题描述

在我的任务中,有必要根据 OU 名称创建组,同时考虑到嵌套。

比如有这样一个OU

OU=Credential,OU=DepIT,DC=Contoso,DC=loc

那么我需要创建以下组:

最后,DepIT_Credential_R并且DepIT_Credential_W必须是 的成员DepIT_L

现在结构如下: OU DepIT,其中 OU Credential。我准备了我想做的截图:

1. DepIT - 结构

2. DepIT - 结构和组

3. 凭证 - 组

4. DepIT_L 组成员

到目前为止我的尝试:

$OU = "OU=DepIT,DC=Contoso,DC=loc"
$one_level_OU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope OneLevel -filter *


$one_level_OU | 

foreach{

New-ADGroup -Name $($_.Name+"_RW") -GroupCategory Security -groupscope global -Path $_.DistinguishedName
New-ADGroup -Name $($_.Name+"_R") -GroupCategory Security -groupscope global -Path $_.DistinguishedName
New-ADGroup -Name $($_.Name+"_L") -GroupCategory Security -groupscope global -Path $_.DistinguishedName

    $ous = Get-ADOrganizationalUnit -SearchBase "OU=$($_.name),OU=DepIT,DC=Contoso,DC=loc" -SearchScope Subtree -filter "name -notlike '*$($one_level_OU.Name)*'"

    $group_L = get-adgroup -Filter "name -like '*_L'" -Properties * | select name

    $ous | foreach {

       New-ADGroup -Name $($_.Name+"_RW") -GroupCategory Security -groupscope global -Path $_.DistinguishedName
       New-ADGroup -Name $($_.Name+"_R") -GroupCategory Security -groupscope global -Path $_.DistinguishedName


       foreach ($gr in $group_L) {
            Add-ADGroupMember -Identity $($_.name) -Members "$($_.Name+"_RW")", "$($_.Name+"_R")"
       }
    }
}

非常感谢您的帮助。

标签: powershellforeachscriptingactive-directoryou

解决方案


推荐阅读