代码实现

  1. # 设置标题
  2. $host.ui.RawUI.WindowTitle="卸载更新及设置WSUS"
  3. Clear-Host
  4. ################################# 卸载更新 ###################################
  5. $KBNUM="5006672", "5006670"
  6. $KBNUM|ForEach-Object{
  7. # -ErrorAction "SilentlyContinue": 抑制内置的错误消息
  8. $hf = Get-HotFix -Id KB$_ -ErrorAction "SilentlyContinue"
  9. if($hf){
  10. # 卸载更新
  11. wusa.exe /uninstall /kb:$_
  12. Write-Host "已卸载更新KB5006672和KB5006670..."
  13. }
  14. }
  15. ################################ 设置WSUS_Server #############################
  16. $WindowsUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\"
  17. $AutoUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"
  18. # $wuserver = Get-ItemProperty -Path $WindowsUpdatePath -Name WUServer | select wuserver
  19. $wuserver = (Get-ItemProperty -Path $WindowsUpdatePath -Name WUServer -ErrorAction "SilentlyContinue").wuserver
  20. # 设置本地wsus_server信息
  21. function LocalUpdateServer() {
  22. Set-ItemProperty -Path $WindowsUpdatePath -Name AcceptTrustedPublisherCerts -Value 1
  23. Set-ItemProperty -Path $WindowsUpdatePath -Name UpdateServiceUrlAlternate -Value "http://wsus-srv.im30.com:8530"
  24. Set-ItemProperty -Path $WindowsUpdatePath -Name WUServer -Value "http://wsus-srv.im30.com:8530"
  25. Set-ItemProperty -Path $WindowsUpdatePath -Name WUStatusServer -Value "http://wsus-srv.im30.com:8530"
  26. }
  27. # 设置wsus执行计划,每周四19点执行更新
  28. function Set_WSUS() {
  29. # 参考手册:https://docs.microsoft.com/zh-CN/windows/deployment/update/waas-wu-settings
  30. Set-ItemProperty -Path $AutoUpdatePath -Name AUOptions -Value 4
  31. # 19点执行操作
  32. Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallTime -Value 19
  33. Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoUpdate -Value 0
  34. # 每周四执行计划
  35. Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallDay -Value 5
  36. Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallEveryWeek -Value 1
  37. Set-ItemProperty -Path $AutoUpdatePath -Name DetectionFrequencyEnabled -Value 1
  38. # 检测更新频率 22小时
  39. Set-ItemProperty -Path $AutoUpdatePath -Name DetectionFrequency -Value 22
  40. # 不自动重启
  41. Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoRebootWithLoggedOnUsers -Value 1
  42. # 使用wsusserver
  43. Set-ItemProperty -Path $AutoUpdatePath -Name UseWUServer -Value 1
  44. # 再次提示重启
  45. Set-ItemProperty -Path $AutoUpdatePath -Name RebootRelaunchTimeoutEnabled -Value 1
  46. Set-ItemProperty -Path $AutoUpdatePath -Name RebootRelaunchTimeout -Value 60
  47. }
  48. if(!$wuserver){
  49. if(Test-Path -Path $WindowsUpdatePath) {
  50. Remove-Item -Path $WindowsUpdatePath -Recurse
  51. }
  52. $null = New-Item -Path $WindowsUpdatePath
  53. $null = New-Item -Path $AutoUpdatePath
  54. # 调用函数
  55. LocalUpdateServer
  56. Set_WSUS
  57. }
  58. Write-Host
  59. # 按任意键退出
  60. function Pause(){
  61. [System.Console]::Write('执行完毕,按任意键退出...')
  62. [void][System.Console]::ReadKey(1)
  63. }
  64. pause

说明

  1. 用到了设置标题:$host.ui.RawUI.WindowTitle="卸载更新及设置WSUS"
  2. 用到了忽略内置报错信息:-ErrorAction "SilentlyContinue"
  3. 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