Advanced Active Directory Replication and Topology Management Using Windows PowerShell (Level 200)

适用范围:Windows Server 2022、Windows Server 2019、Windows Server 2016、Windows Server 2012 R2、Windows Server 2012

本主题更详细地解释了 AD DS 复制和拓扑管理 cmdlet,并提供了其他示例。 有关简介,请参阅使用 Windows PowerShell 的 Active Directory 复制和拓扑管理(级别 100)简介

  1. 介绍

  2. 复制和元数据

  3. Get-ADReplicationAttributeMetadata

  4. Get-ADReplicationPartnerMetadata

  5. Get-ADReplicationFailure

  6. Get-ADReplicationQueueOperation 和 Get-ADReplicationUpToDatenessVectorTable

  7. Sync-ADObject

  8. 拓扑

简介

下表列出了添加到 Active Directory Windows PowerShell 模块的复制和拓扑 cmdlet:

Cmdlet 说明
Get-ADReplicationAttributeMetadata 返回对象的属性复制元数据
Get-ADReplicationConnection 返回域控制器连接对象详细信息
Get-ADReplicationFailure 返回域对象的最新复制故障
Get-ADReplicationPartnerMetadata 返回域控制器的复制配置
Get-ADReplicationQueueOperation 返回当前复制队列积压工作
Get-ADReplicationSite 返回站点信息
Get-ADReplicationSiteLink 返回站点链接信息
Get-ADReplicationSiteLinkBridge 返回站点链接桥信息
Get-ADReplicationSubnet 返回 AD 子网信息
Get-ADReplicationUpToDatenessVectorTable 返回域控制器的 UTD 向量
Get-ADTrust 返回有关域间和林间信任的信息
New-ADReplicationSite 创建新站点
New-ADReplicationSiteLink 创建新站点链接
New-ADReplicationSiteLinkBridge 创建新站点链接桥
New-ADReplicationSubnet 创建新 AD 子网
Remove-ADReplicationSite 删除站点
Remove-ADReplicationSiteLink 删除站点链接
Remove-ADReplicationSiteLinkBridge 删除站点链接桥
Remove-ADReplicationSubnet 删除 AD 子网
Set-ADReplicationConnection 修改连接
Set-ADReplicationSite 修改站点
Set-ADReplicationSiteLink 修改站点链接
Set-ADReplicationSiteLinkBridge 修改站点链接桥
Set-ADReplicationSubnet 修改 AD 子网
Sync-ADObject 强制复制单个对象

其中大部分 cmdlet 在 Repadmin.exe 中都具有其基础。 其他 cmdlet(未列出)处理诸如动态访问控制和组托管服务帐户的功能。

要获取所有 Active Directory Windows PowerShell cmdlet 的完整列表,请运行:

Get-Command -module ActiveDirectory

要获取所有 Active Directory Windows PowerShell cmdlet 参数的完整列表,请参考帮助。 例如:

Get-Help New-ADReplicationSite

使用 Update-Help cmdlet 下载和安装帮助文件

复制和元数据

Repadmin.exe 验证 Active Directory 复制的运行状况和一致性。 Repadmin.exe 提供简单的数据操作选项(例如,某些参数支持 CSV 输出),但自动化通常需要通过文本文件输出进行分析。 为了提供允许真正控制返回的数据的选项,将首先尝试 Windows PowerShell 的 Active Directory 模块;在此之前,你必须创建脚本或使用第三方工具。

此外,以下 cmdlet 实现 TargetScopeEnumerationServer 的新参数集:

  • Get-ADReplicationFailure

  • Get-ADReplicationPartnerMetadata

  • Get-ADReplicationUpToDatenessVectorTable

Target 参数接受以逗号分隔的字符串列表,这些字符串可标识由 Scope 参数指定的目标服务器、站点、域或林。 还允许使用星号 (*),它表示指定范围内的所有服务器。 如果没有指定范围,则表示当前用户林中的所有服务器。 Scope 参数指定搜索的范围。 可接受的值是 ServerSiteDomainForestEnumerationServer 指定用于枚举在 TargetScope 中指定的域控制器列表的服务器。 它的操作方式与 Server 参数相同,并且要求指定的服务器运行 Active Directory Web 服务。

若要引入 cmdlet,下面提供了一些用于显示不适用于 repadmin.exe 的功能的示例方案;有了这些图示,可能实现的管理功能便显而易见。 查看有关特定用法要求的 cmdlet 帮助。

Get-ADReplicationAttributeMetadata

此 cmdlet 类似于 repadmin.exe /showobjmeta。 它允许你返回复制元数据(例如在属性发生更改时)、原始域控制器、版本和 USN 信息以及属性数据。 此 cmdlet 对于审核发生更改的位置和时间非常有用。

与 Repadmin 不同,Windows PowerShell 提供了灵活搜索和输出控制。 例如,可以输出以可读列表形式排列的 Domain Admins 对象的元数据:

Get-ADReplicationAttributeMetadata -object "cn=domain admins,cn=users,dc=corp,dc=contoso,dc=com" -server dc1.corp.contoso.com -showalllinkedvalues | format-list

Screenshot that shows the metadata output of the Domain Admins Object ordered as a readable list.

或者,可以在表中将该数据排列为类似于 repadmin 的形式:

Get-ADReplicationAttributeMetadata -object "cn=domain admins,cn=users,dc=corp,dc=contoso,dc=com" -server dc1.corp.contoso.com -showalllinkedvalues | format-table -wrap

Screenshot that shows the data arranged to look like repadmin in a table.

或者,你可以使用筛选器(例如所有组)通过管道传送 Get-Adobject cmdlet 来为整个对象类获取元数据,然后再加上一个特定日期。 管道是在多个 cmdlet 之间用于传递数据的一个通道。 若要查看在 2012 年 1 月 13 日以某种方式修改的所有组:

Get-ADObject -filter 'objectclass -eq "group"' | Get-ADReplicationAttributeMetadata -server dc1.corp.contoso.com | where-object {$_.lastoriginatingchangetime -like "*1/13/2012*" -and $_.attributename -eq "name"} | format-table object

Screenshot that shows how to see all groups modified in some fashion on January 13th, 2012.

有关管道的更多 Windows PowerShell 操作的详细信息,请参阅 Windows PowerShell 中的管道系统和管道

或者,若要了解每个包含 Tony Wang 这一成员的组,以及该组的最后修改时间:

Get-ADObject -filter 'objectclass -eq "group"' | Get-ADReplicationAttributeMetadata -server dc1.corp.contoso.com -showalllinkedvalues | where-object {$_.attributevalue -like "*tony wang*"} | format-table object,LastOriginatingChangeTime,version -auto

Screenshot that shows how to find out every group that has Tony Wang as a member and when the group was last modified.

或者,若要基于人为高版本,在域中查找所有使用系统状态备份授权还原的对象:

Get-ADObject -filter 'objectclass -like "*"' | Get-ADReplicationAttributeMetadata -server dc1.corp.contoso.com | where-object {$_.version -gt "100000" -and $_.attributename -eq "name"} | format-table object,LastOriginatingChangeTime

Screenshot that shows how to find all objects authoritatively restored using a system state backup in the domain, based on their artificially high version.

或者,将所有用户元数据发送到 CSV 文件,以供以后在 Microsoft Excel 中进行检查:

Get-ADObject -filter 'objectclass -eq "user"' | Get-ADReplicationAttributeMetadata -server dc1.corp.contoso.com -showalllinkedvalues | export-csv allgroupmetadata.csv

Get-ADReplicationPartnerMetadata

此 cmdlet 返回有关域控制器复制的配置和状态信息,从而允许你监视、盘存或解决问题。 与 Repadmin.exe 不同,使用 Windows PowerShell 意味着你仅能以所需格式看到对你而言非常重要的数据。

例如,单个域控制器的可读复制状态:

Get-ADReplicationPartnerMetadata -target dc1.corp.contoso.com

Screenshot that shows how to get the readable replication state of a single domain controller.

或者,域控制器最后一次复制入站及其合作伙伴的时间(采用表格式):

Get-ADReplicationPartnerMetadata -target dc1.corp.contoso.com | format-table lastreplicationattempt,lastreplicationresult,partner -auto

Screenshot that shows the last time a domain controller replicated inbound and its partners, in a table format.

或者,联系林中的所有域控制器,并显示上次尝试复制失败(出于任何原因)的任何域控制器:

Get-ADReplicationPartnerMetadata -target * -scope server | where {$_.lastreplicationresult -ne "0"} | ft server,lastreplicationattempt,lastreplicationresult,partner -auto

Screenshot that shows how to contact all domain controllers in the forest and display any whose last attempted replication failed for any reason.

Get-ADReplicationFailure

此 cmdlet 可用于返回有关复制中最近出现的错误的信息。 它类似于 Repadmin.exe /showreplsum,但同样由于 Windows PowerShell,它可以更好地进行控制。

例如,可以返回域控制器最近发生的故障以及联系失败的合作伙伴:

Get-ADReplicationFailure dc1.corp.contoso.com

Screenshot that shows how you can return a domain controller's most recent failures and the partners he failed contacting.

或者,返回特定 AD 逻辑站点中的所有服务器的对应表视图,已对该视图排序以便进行查看,它仅包含最关键的数据:

Get-ADReplicationFailure -scope site -target default-first-site-name | format-table server,firstfailuretime,failurecount,lasterror,partner -auto

Screenshot that shows how to return a table view for all servers in a specific AD logical site, ordered for easier viewing and containing only the most critical data.

Get-ADReplicationQueueOperation 和 Get-ADReplicationUpToDatenessVectorTable

这两个 cmdlet 都将返回域控制器的更多方面以及它是否是最新的,其中包括挂起的复制和版本矢量信息。

Sync-ADObject

此 cmdlet 类似于运行 Repadmin.exe /replsingleobject。 当你进行需要带外复制的更改(特别是修复某个问题)时,它将非常有用。

例如,如果某人删除了 CEO 的用户帐户,然后使用 Active Directory 回收站还原它,你可能希望将它立即复制到所有域控制器。 你可能还希望在不强制复制所做的所有其他对象更改的情况下执行此操作;毕竟这是你制定复制计划的原因 - 避免重载 WAN 链接。

Get-ADDomainController -filter * | foreach {Sync-ADObject -object "cn=tony wang,cn=users,dc=corp,dc=contoso,dc=com" -source dc1 -destination $_.hostname}

Screenshot that shows how to replicate a deleted account from the Active Directory Recycle Bin to all domain controllers without forcing replication of all the other object changes made.

拓扑

虽然 Repadmin.exe 能够很好地返回有关复制拓扑(例如站点、站点链接、站点链接桥和连接)的信息,但它没有可进行更改的完整参数集。 事实上,从来没有任何可编写脚本的内部 Windows 实用工具可专供管理员创建和修改 AD DS 拓扑。 由于 Active Directory 在数以百万计的客户环境中日益成熟,因此批量修改 Active Directory 逻辑信息的需求也日益凸显。

例如,在快速扩张新的分支机构以及合并其他机构之后,你可能需要基于物理位置、网络更改和新容量要求来更改上百个站点。 可自动进行更改,而不是使用 Dssites.msc 和 Adsiedit.msc。 当你使用由网络和设施团队提供的数据电子表格开始操作时,这一点尤为引人注目。

Get-Adreplication\* cmdlet 可返回有关复制拓扑的信息,并且可用于通过管道批量传送到 Set-Adreplication\* cmdlet 中。 Get cmdlet 不会更改数据,它们仅显示数据或创建可通过管道传送到 Set-Adreplication\* cmdlet 的 Windows PowerShell 会话对象。 NewRemove cmdlet 用于创建或删除 Active Directory 拓扑对象。

例如,你可以使用 CSV 文件创建新站点:

Import-Csv -path C:\newsites.csv | new-adreplicationsite

Screenshot that shows the Notepad interface.

Screenshot that shows how to create new sites using a CSV file.

或者,使用自定义的复制时间间隔和站点成本在两个现有站点之间创建新站点链接:

New-ADReplicationSiteLink -name "chicago<-->waukegan" -sitesincluded chicago,waukegan -cost 50 -replicationfrequencyinminutes 15

Screenshot that shows create a new site link between two existing sites with a custom replication interval and site cost.

或者,在林中找到每个站点,并将其 Options 属性替换为该标志以启用站点间更改通知,从而通过压缩以最大速度进行复制:

Get-ADReplicationSiteLink -filter * | set-adobject -replace @{options=$($_.options -bor 1)}

advanced management with powershell

重要

也可以设置 -bor 5 以在这些站点链接上禁用压缩。

或者,查找缺少子网分配的所有站点,以便协调该列表与这些位置的实际子网:

Get-ADReplicationSite -filter * -property subnets | where-object {!$_.subnets -eq "*"} | format-table name

Screenshot that shows how to find all sites missing subnet assignments, in order to reconcile the list with the actual subnets of those locations.

另请参阅

Introduction to Active Directory Replication and Topology Management Using Windows PowerShell (Level 100)