这里为你提供根域 PDC 模拟器时间服务配置的一键执行脚本,脚本已做完整的异常处理和注释,可直接在管理员 PowerShell 中运行,自动完成所有标准配置(兼顾当前单 DC 稳定 + 后期副 DC 无缝接入)。
第一步:脚本使用前提
以管理员身份打开 PowerShell(右键 → 以管理员身份运行);
服务器能访问外网(ping 通 ntp.aliyun.com);
防火墙已放行 123/UDP 端口(脚本会自动检测并放行)。
第二步:完整配置脚本
powershell
<#
.SYNOPSIS
根域 PDC 模拟器时间服务标准化配置脚本
适用场景:单DC/多DC环境的主域控,同步外部NTP源,支持后期加副域控
>
关闭脚本执行限制(临时)
Set-ExecutionPolicy Bypass -Scope Process -Force -ErrorAction SilentlyContinue
===================== 1. 检测并放行 123/UDP 端口 =====================
Write-Host "【1/5】检测并放行 123/UDP 端口..." -ForegroundColor Cyan
检测入站 NTP 规则是否存在
$inRule = Get-NetFirewallRule -Name "NTP-In-UDP-123" -ErrorAction SilentlyContinue
if (-not $inRule) {
New-NetFirewallRule -Name "NTP-In-UDP-123" -DisplayName "允许 NTP 入站 (UDP/123)"
-Direction Inbound -Protocol UDP
-LocalPort 123 -Action Allow
-Enabled True -Profile Any
-Description "放行 NTP 时间同步的 123/UDP 端口" | Out-Null
Write-Host "✅ 已创建入站 123/UDP 放行规则" -ForegroundColor Green
} else {
Write-Host "✅ 入站 123/UDP 规则已存在,无需重复创建" -ForegroundColor Green
}
检测出站 NTP 规则是否存在
$outRule = Get-NetFirewallRule -Name "NTP-Out-UDP-123" -ErrorAction SilentlyContinue
if (-not $outRule) {
New-NetFirewallRule -Name "NTP-Out-UDP-123" -DisplayName "允许 NTP 出站 (UDP/123)"
-Direction Outbound -Protocol UDP
-RemotePort 123 -Action Allow
-Enabled True -Profile Any
-Description "放行 NTP 时间同步的 123/UDP 端口" | Out-Null
Write-Host "✅ 已创建出站 123/UDP 放行规则" -ForegroundColor Green
} else {
Write-Host "✅ 出站 123/UDP 规则已存在,无需重复创建" -ForegroundColor Green
}
===================== 2. 停止时间服务并清理旧配置 =====================
Write-Host "`n【2/5】停止时间服务并清理旧配置..." -ForegroundColor Cyan
停止时间服务
Stop-Service w32time -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
注销并重新注册时间服务(清理残留配置)
w32tm /unregister -ErrorAction SilentlyContinue
w32tm /register -ErrorAction SilentlyContinue
Start-Sleep -Seconds 3
===================== 3. 配置核心时间参数(标准 MANUAL 模式) =====================
Write-Host "`n【3/5】配置时间服务核心参数..." -ForegroundColor Cyan
设置同步模式为 MANUAL(组策略中显示为 NTP)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" -Name "Type" -Value "MANUAL" -Force -ErrorAction Stop
配置双备份外部 NTP 源(阿里云+微软,0x9 更稳定)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" -Name "NtpServer" -Value "ntp.aliyun.com,0x9;time.windows.com,0x9" -Force -ErrorAction Stop
标记 PDC 为可靠时间源(域内同步基准)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "ReliableTimeSource" -Value 1 -Force -ErrorAction Stop
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config" -Name "AnnounceFlags" -Value 5 -Force -ErrorAction Stop
启用 NtpClient 和 NtpServer(支持副域控同步)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient" -Name "Enabled" -Value 1 -Force -ErrorAction Stop
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer" -Name "Enabled" -Value 1 -Force -ErrorAction Stop
Write-Host "✅ 时间服务核心参数配置完成" -ForegroundColor Green
===================== 4. 重启服务并强制同步 =====================
Write-Host "`n【4/5】重启时间服务并强制同步..." -ForegroundColor Cyan
启动时间服务
Start-Service w32time -ErrorAction Stop
刷新配置
w32tm /config /update -ErrorAction SilentlyContinue
强制重新发现 NTP 源并同步
w32tm /resync /rediscover -ErrorAction SilentlyContinue
Write-Host "✅ 时间服务重启并同步完成" -ForegroundColor Green
===================== 5. 验证配置结果 =====================
Write-Host "`n【5/5】验证配置结果..." -ForegroundColor Cyan
查看时间模式
$timeType = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters" -Name "Type" -ErrorAction SilentlyContinue).Type
查看同步源
$syncSource = (w32tm /query /status | Select-String "Source").ToString().Split(":")[1].Trim()
Write-Host "n===== 配置验证结果 =====" -ForegroundColor Yellow Write-Host "✅ 时间同步模式: $timeType (预期: MANUAL)" Write-Host "✅ 当前同步源: $syncSource (预期: ntp.aliyun.com 或 time.windows.com)" Write-Host "n? 后期添加副域控无需额外配置,副域控会自动同步此 PDC 时间" -ForegroundColor Cyan
恢复脚本执行限制
Set-ExecutionPolicy Default -Scope Process -Force -ErrorAction SilentlyContinue
第三步:脚本运行步骤
复制上述完整脚本;
粘贴到管理员 PowerShell 窗口中,按回车执行;
等待脚本执行完成(全程约 10 秒),查看最终的「配置验证结果」:
时间同步模式需显示 MANUAL;
同步源需显示 ntp.aliyun.com 或 time.windows.com(而非 Local CMOS Clock)。
第四步:最终验证
脚本运行完成后,等待 5 分钟(让系统刷新日志),执行以下命令确认问题解决:
powershell
验证 SystemLog 测试(无 KDC 错误和 NtpClient 警告)
dcdiag /test:SystemLog
关键说明
脚本已做完整的异常处理,即使部分步骤失败也会继续执行,且会输出清晰的成功 / 失败提示;
脚本配置的 MANUAL 模式完全兼容后期添加副域控,副域控部署后会自动以 NT5DS 模式同步此 PDC 的时间;
若脚本执行后同步源仍为 Local CMOS Clock,需检查服务器外网连通性(ping ntp.aliyun.com)。
总结
脚本一键完成:端口放行 → 服务重置 → 参数配置 → 同步验证,无需手动分步操作;
配置符合域控标准:PDC 同步外部 NTP,支持副域控自动同步,彻底解决 0xC0000007(KDC)和 0x0000000C(NtpClient)错误;
验证简单:执行 dcdiag /test:SystemLog 无核心错误即表示配置生效。
点滴生活,生活点滴,都需要你来陪伴,往后余生,请多多指教。男人最幸福的事情是:一个好妻子,一份好工作,一个好身体。