ESC

開始輸入,可搜尋發票、服務、域名、工單,以及 更多...

搜尋... Ctrl+K
Windows 伺服器

Windows Server 更新管理:關閉自動重啟、設定活動時間與 PowerShell 安裝更新

6 個步驟 8 分鐘閱讀 17 次閱讀 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、作業系統版本、執行過的命令和報錯截圖,方便工程師快速定位問題。

這篇文章有幫助嗎?

相關教程