代码实现
# 设置标题$host.ui.RawUI.WindowTitle="卸载更新及设置WSUS"Clear-Host################################# 卸载更新 ###################################$KBNUM="5006672", "5006670"$KBNUM|ForEach-Object{# -ErrorAction "SilentlyContinue": 抑制内置的错误消息$hf = Get-HotFix -Id KB$_ -ErrorAction "SilentlyContinue"if($hf){# 卸载更新wusa.exe /uninstall /kb:$_Write-Host "已卸载更新KB5006672和KB5006670..."}}################################ 设置WSUS_Server #############################$WindowsUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\"$AutoUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"# $wuserver = Get-ItemProperty -Path $WindowsUpdatePath -Name WUServer | select wuserver$wuserver = (Get-ItemProperty -Path $WindowsUpdatePath -Name WUServer -ErrorAction "SilentlyContinue").wuserver# 设置本地wsus_server信息function LocalUpdateServer() {Set-ItemProperty -Path $WindowsUpdatePath -Name AcceptTrustedPublisherCerts -Value 1Set-ItemProperty -Path $WindowsUpdatePath -Name UpdateServiceUrlAlternate -Value "http://wsus-srv.im30.com:8530"Set-ItemProperty -Path $WindowsUpdatePath -Name WUServer -Value "http://wsus-srv.im30.com:8530"Set-ItemProperty -Path $WindowsUpdatePath -Name WUStatusServer -Value "http://wsus-srv.im30.com:8530"}# 设置wsus执行计划,每周四19点执行更新function Set_WSUS() {# 参考手册:https://docs.microsoft.com/zh-CN/windows/deployment/update/waas-wu-settingsSet-ItemProperty -Path $AutoUpdatePath -Name AUOptions -Value 4# 19点执行操作Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallTime -Value 19Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoUpdate -Value 0# 每周四执行计划Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallDay -Value 5Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallEveryWeek -Value 1Set-ItemProperty -Path $AutoUpdatePath -Name DetectionFrequencyEnabled -Value 1# 检测更新频率 22小时Set-ItemProperty -Path $AutoUpdatePath -Name DetectionFrequency -Value 22# 不自动重启Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoRebootWithLoggedOnUsers -Value 1# 使用wsusserverSet-ItemProperty -Path $AutoUpdatePath -Name UseWUServer -Value 1# 再次提示重启Set-ItemProperty -Path $AutoUpdatePath -Name RebootRelaunchTimeoutEnabled -Value 1Set-ItemProperty -Path $AutoUpdatePath -Name RebootRelaunchTimeout -Value 60}if(!$wuserver){if(Test-Path -Path $WindowsUpdatePath) {Remove-Item -Path $WindowsUpdatePath -Recurse}$null = New-Item -Path $WindowsUpdatePath$null = New-Item -Path $AutoUpdatePath# 调用函数LocalUpdateServerSet_WSUS}Write-Host# 按任意键退出function Pause(){[System.Console]::Write('执行完毕,按任意键退出...')[void][System.Console]::ReadKey(1)}pause
说明
- 用到了设置标题:
$host.ui.RawUI.WindowTitle="卸载更新及设置WSUS"; - 用到了忽略内置报错信息:
-ErrorAction "SilentlyContinue"; - powershell上执行pause命令不生效,可调用系统控制台方法: ```powershell function Pause(){
[System.Console]::Write('执行完毕,按任意键退出...')
[void][System.Console]::ReadKey(1)
} pause
Powershell 识别和处理异常:[https://www.pstips.net/powershell-recognize-and-response-errors.html](https://www.pstips.net/powershell-recognize-and-response-errors.html)
<a name="K7pM5"></a>
## 系统控制台方法
```powershell
function Pause(){
[System.Console]::Write('按任意键继续...')
[void][System.Console]::ReadKey(1)
}
[System.Console]::Title='控制台常用方法'
0..15|ForEach-Object{
[System.Console]::SetCursorPosition($_,$_)
[System.Console]::BackgroundColor=15-$_
[System.Console]::ForegroundColor=$_
$_
[System.Console]::SetCursorPosition(15+$_,15-$_)
[System.Console]::BackgroundColor=$_
[System.Console]::ForegroundColor=15-$_
15-$_
}
[System.Console]::ResetColor()
[System.Console]::SetWindowSize(80,30)
[System.Console]::SetCursorPosition(0,16)
pause
