当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在 Active Directory 中创建组织单位 (OU) 结构

作者:精品下载站 日期:2024-12-14 23:24:42 浏览:18 分类:玩电脑

使用 PowerShell 在 Active Directory 中创建组织单位 (OU) 结构


通常,在创建新的组织单位 (OU) 时,Active Directory 管理员必须在新的 OU 内创建嵌套容器的结构。例如,当公司开设新的分支机构时,您需要为新OU中的用户、组、服务器和服务帐户创建AD容器。为了避免在 ADUC 管理单元 (dsa.msc) 中手动创建嵌套 OU 并分配权限,您可以使用本文中的 PowerShell 脚本批量创建和配置嵌套 OU。该脚本不仅创建新的组织单位,还创建管理安全组并授予它们对新 OU 的权限。

要在 Active Directory 中创建新 OU,请使用 RSAT-AD-PowerShell 模块中的 New-ADOrganizationalUnit cmdlet。要在指定容器中创建OU,可以使用以下命令:

Import-Module ActiveDirectory
New-ADOrganizationalUnit -Name "Users" -Path "OU=Berlin,OU=DE,DC=a-d,DC=com" -Description "Account container for Berlin users" -PassThru

如果不指定-Path参数,则会在 AD 根目录中创建一个新的 OU。

默认情况下,创建 AD 容器时,会为其启用 ProtectedFromAccidentalDeletion 选项。您可以禁用它或使用以下命令更改 Active Directory 中的任何其他组织单位属性:

Set-ADOrganizationalUnit

小命令:

Get-ADOrganizationalUnit -Identity “OU=Users,OU=Berlin,OU=DE,DC=a-d,DC=com”| Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $false

要删除包含所有嵌套对象的 OU,请使用以下命令:

Remove-ADObject -Identity "OU=Users,OU=Berlin,OU=DE,DC=a-d,DC=com” -Recursive

让我们看一下一个小型 PowerShell 脚本,用于在 Active Directory 中为新公司分支机构自动创建典型的 OU 结构。

假设您的公司的每个分支机构都有一个单独的 OU,具有以下嵌套容器结构:

Country
-City_name
--Admins
--Computers
--Contacts
--Groups
--Servers
--Service Accounts
--Users

创建 OU 结构后,您需要在 AD 中创建分支管理员组,并授予他们对新 OU 的权限:

  • City_admins - 分支机构管理员(完全控制所有分支机构 OU);

  • City_account_managers - 具有帐户操作员权限的用户(创建新用户、AD 密码重置等)。仅将权限分配给用户和服务帐户 OU;

  • City_wks_admins - 在分支机构计算机上具有管理员权限的帮助台支持团队(分配计算机 OU 上的权限,通过 GPO 将此组添加到计算机上的本地管理员组)。

[玩转系统] 使用 PowerShell 在 Active Directory 中创建组织单位 (OU) 结构

以下是此 PowerShell 脚本的基本版本以及注释。 CreateBranchOUs.ps1 脚本代码可在我的 GitHub 存储库中找到:

#Create Active Directory OU structure, security groups and assign permissions for a new branch office
# Set the container name
$Country="DE"
$City = "HH"
$CityFull="Hamburg"
$DomainDN=(Get-ADDomain).DistinguishedName
$ParentOU= "OU="+$Country+",$DomainDN"
$OUs = @(
"Admins",
"Computers",
"Contacts",
"Groups",
"Servers",
"Service Accounts",
"Users"
)
# Create an OU for a new branch office
$newOU=New-ADOrganizationalUnit -Name $CityFull -path $ParentOU -Description “A container for $CityFull users” -PassThru
ForEach ($OU In $OUs) {
New-ADOrganizationalUnit -Name $OU -Path $newOU
}
#Create administrative groups
$adm_grp=New-ADGroup ($City+ "_admins") -path ("OU=Admins,OU="+$CityFull+","+$ParentOU) -GroupScope Global -PassThru -Verbose
$adm_wks=New-ADGroup ($City+ "_account_managers") -path ("OU=Admins,OU="+$CityFull+","+$ParentOU) -GroupScope Global -PassThru -Verbose
$adm_account=New-ADGroup ($City+ "_wks_admins") -path ("OU=Admins,OU="+$CityFull+","+$ParentOU) -GroupScope Global -PassThru -Verbose
##### An example of assigning password reset permissions for the _account_managers group on the Users OU
$confADRight = "ExtendedRight"
$confDelegatedObjectType = "bf967aba-0de6-11d0-a285-00aa003049e2" # User Object Type GUID
$confExtendedRight = "00299570-246d-11d0-a768-00aa006e0529" # Extended Right PasswordReset GUID
$acl=get-acl ("AD:OU=Users,OU="+$CityFull+","+$ParentOU)
$adm_accountSID = [System.Security.Principal.SecurityIdentifier]$adm_account.SID
#Build an Access Control Entry (ACE)string
$aceIdentity = [System.Security.Principal.IdentityReference] $adm_accountSID
$aceADRight = [System.DirectoryServices.ActiveDirectoryRights] $confADRight
$aceType = [System.Security.AccessControl.AccessControlType] "Allow"
$aceInheritanceType = [System.DirectoryServices.ActiveDirectorySecurityInheritance] "Descendents"
$ace = New-Object System.DirectoryServices.ActiveDirectoryAccessRule($aceIdentity, $aceADRight, $aceType, $confExtendedRight, $aceInheritanceType,$confDelegatedObjectType)
# Apply ACL
$acl.AddAccessRule($ace)
Set-Acl -Path ("AD:OU=Users,OU="+$CityFull+","+$ParentOU) -AclObject $acl

在脚本开头设置变量并运行它(不要忘记检查当前的 PowerShell 执行策略设置)。该脚本将创建您需要的 OU 结构和组,并将密码重置权限委托给用户 OU。

[玩转系统] 使用 PowerShell 在 Active Directory 中创建组织单位 (OU) 结构

要分配 AD 权限,请使用 Set-ACL cmdlet,它还允许您分配文件和文件夹的 NTFS 权限。

您可以通过添加为新分支机构创建 OU 时执行的典型操作来扩展脚本。

例如,您可以使用 PowerShell 创建组策略对象 (GPO) 并将它们链接到 OU:

$wksGPO=New-GPO -Name ($City + “_WKS_Policy”) -Comment "$City Workstations Policy"
Get-GPO $wksGPO | New-GPLink -Target ("OU=Computers,OU="+$City+","+$ParentOU) -LinkEnabled Yes

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯