ESC

开始输入,可搜索发票、服务、域名、工单,以及 更多...

搜索... Ctrl+K
Windows 服务器

Windows Server 更新管理:关闭自动重启、设置活动时间与 PowerShell 安装更新

6 个步骤 8 分钟阅读 2 次阅读 0
本文目录

生产环境的 Windows 服务器最怕半夜因为自动更新突然重启,导致网站、数据库或游戏服中断。本文讲解 Windows Server 更新管理的完整做法:用 sconfig、组策略或注册表控制自动更新与自动重启,设置活动时间,用 PowerShell 的 PSWindowsUpdate 模块手动安装更新、查看更新历史,并在维护窗口内计划重启。适用于 Windows Server 2016/2019/2022。

步骤 1:用 sconfig 设置 Windows Update 模式

以管理员身份打开 PowerShell 或命令提示符,输入 sconfig。在菜单中选择“Windows 更新设置”项(2016/2019/2022 中通常为 5),可选自动、仅下载或手动。生产服务器推荐“仅下载”:补丁自动下载,但何时安装、何时重启由你决定。

sconfig

步骤 2:用组策略或注册表关闭 Windows Server 自动重启

图形方式:运行 gpedit.msc,依次打开“计算机配置 → 管理模板 → Windows 组件 → Windows 更新”,将“配置自动更新”设为已启用并选择“3 - 自动下载并通知安装”,再启用“对于有已登录用户的计算机,计划的自动更新安装不执行重启”。不方便使用图形界面时,可以直接写入同等的注册表项:

$au = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
New-Item -Path $au -Force | Out-Null
# 3 = download automatically, notify before install
Set-ItemProperty -Path $au -Name NoAutoUpdate -Type DWord -Value 0
Set-ItemProperty -Path $au -Name AUOptions -Type DWord -Value 3
# Never auto-restart while a user is signed in
Set-ItemProperty -Path $au -Name NoAutoRebootWithLoggedOnUsers -Type DWord -Value 1
gpupdate /force
“有登录用户时不自动重启”只在有用户会话时生效。如果你习惯直接关闭 RDP 窗口(会话仍保留),它可以起作用;但若注销了所有用户,系统仍可能在安装后重启,所以推荐配合 AUOptions=3 一起使用。

步骤 3:设置活动时间,避免业务高峰重启

活动时间内 Windows 不会因为更新自动重启。Windows Server 2016 及以上可在“设置 → 更新和安全 → Windows 更新 → 更改使用时段”中设置,也可以用下面的注册表命令,最长 18 小时。

$ux = "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"
# Active hours 08:00-23:00 (no automatic restart in this window)
Set-ItemProperty -Path $ux -Name ActiveHoursStart -Type DWord -Value 8
Set-ItemProperty -Path $ux -Name ActiveHoursEnd -Type DWord -Value 23
Get-ItemProperty -Path $ux | Select-Object ActiveHoursStart,ActiveHoursEnd

步骤 4:用 PSWindowsUpdate 模块安装 Windows 更新

PSWindowsUpdate 是常用的 PowerShell 更新管理模块,可以在命令行中列出、安装、隐藏补丁,适合 Server Core 和批量运维。Windows Server 2016 需要先启用 TLS 1.2 才能从 PowerShell Gallery 下载模块。-IgnoreReboot 参数保证安装后不会自动重启。

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -Force
Install-Module -Name PSWindowsUpdate -Force
Import-Module PSWindowsUpdate

# List available updates
Get-WindowsUpdate

# Install everything but do NOT reboot automatically
Install-WindowsUpdate -AcceptAll -IgnoreReboot

# Install a single KB only
Install-WindowsUpdate -KBArticleID KB5005112 -AcceptAll -IgnoreReboot

步骤 5:查看 Windows 更新历史与待重启状态

排查“服务器为什么重启了”或确认某个 KB 是否已装上时,可以查看更新历史,并检查是否有补丁在等待重启。

Get-WUHistory -Last 20 | Format-Table Date,KB,Result,Title -AutoSize
Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 10

# Is a reboot pending?
Get-WURebootStatus
Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"

步骤 6:在维护窗口内计划重启

补丁装好后,挑选业务低谷(例如凌晨 3 点)执行重启。下面命令会计算到次日 03:00 的秒数并设置定时重启,需要时可以随时取消。

# Reboot at 03:00 tonight (time in seconds from now)
$t = (Get-Date).Date.AddDays(1).AddHours(3)
shutdown /r /t ([int]($t - (Get-Date)).TotalSeconds) /c "Planned update reboot"
# Cancel a scheduled reboot
shutdown /a
重启前请确认已备份重要数据,并确保服务器上的服务已设置为开机自动启动。重启后如果远程桌面长时间无法连接,可能是系统仍在配置更新,请耐心等待 10–20 分钟再联系技术支持。

常见问题

可以彻底关闭 Windows Update 吗?

技术上可以,但长期不打补丁会让 RDP、SMB 等服务暴露在已知漏洞之下。建议改为“仅下载/通知安装”,每月固定时间手动安装并重启。

sconfig 里的选项编号和本文不一样?

不同版本和累积更新后 sconfig 菜单可能略有调整,请以屏幕上显示的“Windows 更新设置”和“安装更新”文字为准。

更新一直卡在下载或失败怎么办?

先确认服务器能访问外网和 DNS 正常,然后运行 sfc /scannow 与 DISM /Online /Cleanup-Image /RestoreHealth 修复系统组件,再重新安装更新。

如按以上步骤仍无法解决,请提交工单联系 IMIDC 7×24 技术支持,并附上服务器 IP、操作系统版本、执行过的命令和报错截图,方便工程师快速定位问题。

这篇文章有帮助吗?

相关教程