在我的任务中,有必要根据OU名称创建组,并考虑嵌套。
例如,有一个OU
OU=Credential,OU=DepIT,DC=Contoso,DC=loc
然后我需要创建以下组:
在DepIT
中:三组DepIT_R
,DepIT_W
,DepIT_L
在Credential
中:两组DepIT_Credential_R
,DepIT_Credential_W
最后,DepIT_Credential_R
和DepIT_Credential_W
必须是DepIT_L
的成员。
现在结构如下:OU DepIT
,在其中OU Credential
。我已经准备好要执行的操作的屏幕截图:
到目前为止,我的尝试:
$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")"
}
}
}
非常感谢您的帮助。