账户与安全

用于计算对象安全描述符大小的脚本 - Windows Server

· 52CE 我爱测

此脚本可帮助你计算驻留在给定基可分辨名称(DN)下Active Directory对象的ntSecurityDescriptor属性的大小(以字节为单位)。 该脚本将其结果写入 CSV 文件。 脚本 引用 排查 AD 复制错误 8418:由于参与服务器之间的模式不匹配,复制操作失败(../active-directory/replication-error-8418)

此脚本可帮助你计算驻留在给定基可分辨名称(DN)下Active Directory对象的ntSecurityDescriptor属性的大小(以字节为单位)。 该脚本将其结果写入 CSV 文件。

脚本

#This sample script is not supported under any Microsoft standard support 
#program or service. This sample script is provided AS IS without warranty of 
#any kind. Microsoft further disclaims all implied warranties including,
#without limitation, any implied warranties of merchantability or of fitness
#for a particular purpose. The entire risk arising out of the use or 
#performance of the sample scripts and documentation remains with you. In no 
#event shall Microsoft, its authors, or anyone else involved in the creation,
#production, or delivery of the scripts be liable for any damages whatsoever 
#(including, without limitation, damages for loss of business profits, business
#interruption, loss of business information, or other pecuniary loss) arising 
#out of the use of or inability to use this sample script or documentation, 
#even if Microsoft has been advised of the possibility of such damages.

<#

.SYNOPSIS
    Calculates the size (in bytes) of the ntSecurityDescriptor on AD objects under a given base DN,
    and writes results to a CSV file.

.PARAMETER Base
    The LDAP base DN or container to search (e.g., "OU=Users,DC=contoso,DC=com")

.PARAMETER OutputPath
    Path to the CSV file where results will be written.

.PARAMETER MinimumSize
    Optional. Only include objects whose ntSecurityDescriptor size is greater than this threshold (in bytes).

.EXAMPLE
    .\Get-ACLByteSize.ps1 -Base "OU=Users,DC=contoso,DC=com" -OutputPath "C:\Temp\NTSD_Sizes.csv"

.EXAMPLE
    .\Get-ACLByteSize.ps1 -Base "DC=contoso,DC=com" -OutputPath "C:\Temp\NTSD_Sizes.csv" -MinimumSize 60000
#>

param(
    [Parameter(Mandatory = $true)]
    [string]$Base,

    [Parameter(Mandatory = $true)]
    [string]$OutputPath,

    [Parameter(Mandatory = $false)]
    [int]$MinimumSize = 0
)

Import-Module ActiveDirectory -ErrorAction Stop

$results = @()

Write-Host "Enumerating AD objects under $Base ..."

$Objects = Get-ADObject -Filter * -SearchBase $Base -Properties distinguishedName

foreach ($object in $Objects) {
    try {
        # Bind and request SD
        $searcher = [adsisearcher]"(distinguishedName=$($object.DistinguishedName))"
        $searcher.PropertiesToLoad.Add("ntSecurityDescriptor") | Out-Null
        $searcher.SecurityMasks = "Dacl,Owner,Group,sacl"
        $result = $searcher.FindOne()

        if ($null -ne $result -and $result.Properties["ntsecuritydescriptor"].Count -gt 0) {
            $sdBytes = $result.Properties["ntsecuritydescriptor"][0]

            if ($sdBytes -is [byte[]]) {
                $size = $sdBytes.Length
            }
            elseif ($sdBytes -is [System.DirectoryServices.ActiveDirectorySecurity]) {
                $size = ($sdBytes.GetSecurityDescriptorBinaryForm()).Length
            }
            else {
                Write-Warning "Unexpected SD type on $($object.DistinguishedName): $($sdBytes.GetType().FullName)"
                continue
            }

            if ($size -ge $MinimumSize) {
                $results += [pscustomobject]@{
                    DistinguishedName = $object.DistinguishedName
                    ObjectClass       = $object.ObjectClass
                    NTSD_Size_Bytes   = $size
                }
            }
        }
        else {
            Write-Verbose "No ntSecurityDescriptor found for $($object.DistinguishedName)"
        }
    }
    catch {
        Write-Warning "Error processing $($object.DistinguishedName): $_"
    }
}

if ($results.Count -gt 0) {
    $results | Sort-Object NTSD_Size_Bytes -Descending | Export-Csv -Path $OutputPath -NoTypeInformation -Encoding UTF8
    Write-Host "`nExported $($results.Count) results to $OutputPath"
}
else {
    Write-Warning "No results matched your criteria (MinimumSize=$MinimumSize)."
}

引用

排查 AD 复制错误 8418:由于参与服务器之间的模式不匹配,复制操作失败

资料来源与适用范围

资料来源
微软官方中文故障文档
适用范围
按正文的 Windows / Windows Server 适用版本;保留原文兼容性要求
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-10-26
授权说明
CC BY 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。