-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManage-WindowsUpdate.ps1
More file actions
177 lines (156 loc) · 6.78 KB
/
Manage-WindowsUpdate.ps1
File metadata and controls
177 lines (156 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<#
Windows Update Pause Manager - 暂停 / 取消暂停 / 备份 / 还原 / 重启服务
说明:请以管理员权限运行本脚本。
v1.0
#>
# 1. 确保脚本以管理员权限运行
function Ensure-RunAsAdmin {
$current = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal($current)
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "需要管理员权限,正在以管理员身份重新启动..." -ForegroundColor Yellow
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = (Get-Command powershell).Source
$psi.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
$psi.Verb = "runas"
try {
[System.Diagnostics.Process]::Start($psi) | Out-Null
} catch {
Write-Error "以管理员身份启动失败:$($_.Exception.Message)"
}
exit
}
}
# 运行管理员权限检查
Ensure-RunAsAdmin
# 定义脚本所在目录为备份和还原的根目录
$ScriptRoot = $PSScriptRoot
# 2. 暂停更新
function Pause-Updates {
# 使用固定的日期时间字符串
$commands = @(
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v FlightSettingsMaxPauseDays /t REG_DWORD /d 7000 /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseFeatureUpdatesStartTime /t REG_SZ /d "2023-07-07T10:00:52Z" /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseFeatureUpdatesEndTime /t REG_SZ /d "2042-09-05T09:59:52Z" /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseQualityUpdatesStartTime /t REG_SZ /d "2023-07-07T10:00:52Z" /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseQualityUpdatesEndTime /t REG_SZ /d "2042-09-05T09:59:52Z" /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseUpdatesStartTime /t REG_SZ /d "2023-07-07T09:59:52Z" /f',
'reg add "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v PauseUpdatesExpiryTime /t REG_SZ /d "2042-09-05T09:59:52Z" /f'
)
foreach ($c in $commands) {
Write-Host "→ 正在执行..."
cmd.exe /c $c | Out-Null
}
Write-Host "写入完成。。" -ForegroundColor Green
}
# 3. 取消暂停更新
function Unpause-Updates {
$names = @(
'FlightSettingsMaxPauseDays',
'PauseFeatureUpdatesStartTime',
'PauseFeatureUpdatesEndTime',
'PauseQualityUpdatesStartTime',
'PauseQualityUpdatesEndTime',
'PauseUpdatesStartTime',
'PauseUpdatesExpiryTime'
)
foreach ($n in $names) {
$cmd = 'reg delete "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" /v ' + $n + ' /f'
Write-Host "→ 正在删除 $n..."
cmd.exe /c $cmd | Out-Null
}
Write-Host "删除操作完成。" -ForegroundColor Green
}
# 4. 备份设置到脚本目录
function Backup-Settings {
$t = Get-Date -Format 'yyyyMMdd_HHmmss'
$outfile = Join-Path $ScriptRoot "WU_UX_Settings_backup_$t.reg"
$cmd = 'reg export "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings" "' + $outfile + '" /y'
cmd.exe /c $cmd
if ($LASTEXITCODE -eq 0) { Write-Host "备份完成:$outfile" -ForegroundColor Green } else { Write-Warning "备份失败或该键不存在。错误代码:$LASTEXITCODE" }
}
# 5. 列出脚本目录中的备份文件
function List-Backups {
Get-ChildItem -Path $ScriptRoot -Filter 'WU_UX_Settings_backup_*.reg' -File -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending
}
# 6. 从脚本目录的备份中还原
function Restore-Settings {
$backs = List-Backups
if (-not $backs -or $backs.Count -eq 0) {
Write-Warning "在脚本目录中未找到备份文件,请先执行备份。"
return
}
Write-Host "找到以下备份(按序号选择恢复):" -ForegroundColor Cyan
for ($i = 0; $i -lt $backs.Count; $i++) {
$idx = $i + 1
Write-Host "[$idx] $($backs[$i].Name) (修改时间: $($backs[$i].LastWriteTime))"
}
$choice = Read-Host "输入要恢复的序号(或直接粘贴完整文件路径)"
$file = $null
$n = 0
if ([int]::TryParse($choice, [ref]$n)) {
if ($n -ge 1 -and $n -le $backs.Count) {
$file = $backs[$n - 1].FullName
} else {
Write-Warning "序号无效,取消操作。"
return
}
} else {
$file = $choice
}
if (-not (Test-Path $file -PathType Leaf)) {
Write-Warning "找不到文件或路径无效:$file"
return
}
Write-Host "正在从 '$((Get-Item $file).Name)' 导入..." -ForegroundColor Yellow
$cmd = 'reg import "' + $file + '"'
cmd.exe /c $cmd
if ($LASTEXITCODE -eq 0) {
Write-Host "恢复成功。" -ForegroundColor Green
} else {
Write-Warning "恢复可能失败,错误码:$LASTEXITCODE"
}
}
# 7. 重启 Windows Update 服务
function Restart-WindowsUpdateService {
Write-Host "重启 Windows Update 服务 (wuauserv)..." -ForegroundColor Cyan
try {
Stop-Service wuauserv -Force -ErrorAction Stop
Write-Host "服务已停止。"
Start-Sleep -Seconds 2
Start-Service wuauserv -ErrorAction Stop
Write-Host "服务已启动。" -ForegroundColor Green
} catch {
Write-Warning "重启服务失败:$($_.Exception.Message)"
}
}
# 8. 主循环菜单
while ($true) {
Clear-Host
Write-Host "=== Windows Update Pause Manager v1.0 ===" -ForegroundColor Magenta
Write-Host "当前工作目录: $ScriptRoot" -ForegroundColor DarkGray
Write-Host "========================================="
Write-Host "1) 暂停更新 (暂停至2042年)"
Write-Host "2) 取消暂停 (恢复系统默认)"
Write-Host "3) 备份当前设置 (脚本目录)"
Write-Host "4) 还原备份设置 (脚本目录)"
Write-Host "5) 重启 Windows Update 服务"
Write-Host "0) 退出脚本"
Write-Host "========================================="
$sel = Read-Host "请选择操作 (0-5)"
switch ($sel) {
'1' { Pause-Updates }
'2' { Unpause-Updates }
'3' { Backup-Settings }
'4' { Restore-Settings }
'5' { Restart-WindowsUpdateService }
'0' {
Write-Host "正在退出..."
exit
}
default { Write-Host '无效选择,请输入 0-5 之间的数字。' -ForegroundColor Red }
}
Write-Host ""
Write-Host "操作已执行,按回车键返回主菜单..." -ForegroundColor DarkGray
Read-Host | Out-Null
}