[玩转系统] 使用 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 脚本的基本版本以及注释。 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。
要分配 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
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[软件合集] 25年5月28日 精选软件37个
[软件合集] 25年5月27日 精选软件26个
[电影] 毒劫 Havoc(2025)【NF1080P超清】【汤姆·哈迪主演】
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[办公模版] office模板合集:包含word、Excel、PowerPoint、Access四类共计2000多个模板
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[音乐] 华语流行伤感情经典歌无损音乐合集(700多首)
[影视] 内地绝版高清录像带 [mpg]
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag