Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ใช้โมดูล PowerShell PSWindowsUpdate จัดการ Windows Updates จาก Command Line โมดูล PSWindowsUpdate ไม่ได้ Built-in ใน Windows แต่ติดตั้งได้จาก PowerShell Gallery Repository PSWindowsUpdate ช่วยให้ Admin ตรวจสอบ ติดตั้ง ลบ และซ่อน Updates บน Windows Servers และ Workstations แบบ Remote โมดูลนี้มีคุณค่ามากสำหรับจัดการ Updates บน Windows Server Core หรือ Hyper-V Server (ที่ไม่มี GUI) และตอน Configure Windows Image ใน Audit Mode

ติดตั้งโมดูล PSWindowsUpdate

ติดตั้ง PSWindowsUpdate Module บน Windows 10/11 และ Windows Server 2022/2019/2016 จาก Online Repository (PSGallery) ด้วย PackageManagement คำสั่งเดียว:

Install-Module -Name PSWindowsUpdate -Force

หลังติดตั้งเสร็จ ตรวจสอบ Package:

Get-Package -Name PSWindowsUpdate

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ตอนติดตั้งบน Windows เวอร์ชันเก่า (2012R2/Windows 8.1 และต่ำกว่า) อาจได้ Error:

Install-Module: Unable to download from URI. Unable to download the list of available providers. Check your internet connection.

ต้องใช้ TLS 1.2 Protocol สำหรับ Connection เปิดใช้งานสำหรับ PowerShell Session ปัจจุบัน:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

หากมี Windows เวอร์ชันเก่า (Windows 7/8.1/Windows Server 2008 R2/2012 R2) หรือไม่มี Internet Access โดยตรง สามารถติดตั้ง PSWindowsUpdate แบบ Manual ได้ (ดูคู่มือ "How to install PowerShell modules offline?")

  1. ดาวน์โหลด PSWindowsUpdate Module ไปที่คอมพิวเตอร์ Online ใดๆ: Save-Module –Name PSWindowsUpdate –Path C:\ps\
  2. คัดลอก Module ไปโฟลเดอร์บน Target Computer %WINDIR%\System32\WindowsPowerShell\v1.0\Modules จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell
  3. ตั้งค่า PowerShell Script Execution Policy: Set-ExecutionPolicy –ExecutionPolicy RemoteSigned -force
  4. Import Module เข้า PowerShell Session: Import-Module PSWindowsUpdate

หมายเหตุ ใน Windows 7/Windows Server 2008 R2 ตอน Import PSWindowsUpdate Module อาจเกิด Error: The term "Unblock-File" is not recognized as the name of a cmdlet สาเหตุคือ Module ใช้ Functions ที่ปรากฏใน PowerShell 3.0 เท่านั้น ต้อง Update PowerShell Version หรือลบบรรทัด | Unblock-File ออกจากไฟล์ PSWindowsUpdate.psm1 ด้วยตนเอง

หลังติดตั้ง PSWindowsUpdate Module บนคอมพิวเตอร์แล้ว สามารถติดตั้ง Remote บนคอมพิวเตอร์/เซิร์ฟเวอร์อื่นได้ด้วย Cmdlet Update-WUModule ตัวอย่าง คัดลอก PSWindowsUpdate Module จากคอมพิวเตอร์ของคุณไปยัง Remote Hosts 2 เครื่อง รันคำสั่ง (ต้องมี Access ไป Remote Servers ผ่าน WinRM Protocol):

$Targets = "lon-fs02", "lon-db01"
Update-WUModule -ComputerName $Targets –Local

บันทึก (Export) PoSh Module ไป Shared Network Folder เพื่อ Import บนคอมพิวเตอร์อื่นต่อไป:

Save-Module -Name PSWindowsUpdate –Path \\lon-fs02\psmodules\

รายการ Cmdlets ของ PSWindowsUpdate

แสดงรายการ Cmdlets ที่มีใน PSWindowsUpdate Module:

get-command -module PSWindowsUpdate

อธิบายการใช้งาน Module Commands สรุป:

  • Clear-WUJob – ใช้ Get-WUJob ล้าง WUJob ใน Task Scheduler
  • Download-WindowsUpdate (Alias สำหรับ Get-WindowsUpdate –Download) — ดูรายการ Updates และ Download
  • Get-WUInstall, Install-WindowsUpdate (Alias สำหรับ Get-WindowsUpdate –Install) – ติดตั้ง Windows Updates
  • Hide-WindowsUpdate (Alias สำหรับ Get-WindowsUpdate -Hide:$false) – ซ่อน Update
  • Uninstall-WindowsUpdate – ลบ Update ด้วย Remove-WindowsUpdate
  • Add-WUServiceManager – ลงทะเบียน Update Server (Windows Update Service Manager) บนคอมพิวเตอร์
  • Enable-WURemoting — เปิด Windows Defender Firewall Rules อนุญาต Remote Use ของ PSWindowsUpdate Cmdlets
  • Get-WindowsUpdate (Get-WUList) — แสดงรายการ Updates ตรงตาม Criteria ค้นหาและติดตั้ง Updates อนุญาต Download/Install จาก WSUS Server หรือ Microsoft Update เลือก Update Categories, Specific Updates และตั้งกฎ Computer Restart ตอน Install Updates
  • Get-WUApiVersion – ดู Windows Update Agent Version บนคอมพิวเตอร์
  • Get-WUHistory – แสดงรายการ Updates ที่ติดตั้งแล้ว (Update History)
  • Get-WUInstallerStatus — ตรวจสอบ Windows Installer Service Status
  • Get-WUJob – ตรวจสอบ WUJob Update Tasks ใน Task Scheduler
  • Get-WULastResults — วันที่ Search และ Install Updates ล่าสุด (LastSearchSuccessDate และ LastInstallationSuccessDate)
  • Get-WURebootStatus — ตรวจสอบว่าต้อง Reboot เพื่อ Apply Update เฉพาะหรือไม่
  • Get-WUServiceManager – แสดง Update Sources
  • Get-WUSettings – ดู Windows Update Client Settings
  • Invoke-WUJob – เรียก WUJobs Task ใน Task Scheduler ให้รัน PSWindowsUpdate Commands ทันที
  • Remove-WindowsUpdate – Uninstall Update ด้วย KB ID
  • Remove-WUServiceManager – ปิด Windows Update Service Manager
  • Set-PSWUSettings – บันทึก PSWindowsUpdate Module Settings ลง XML File
  • Set-WUSettings – ตั้งค่า Windows Update Client Settings
  • Update-WUModule – อัปเดต PSWindowsUpdate Module (อัปเดตบน Remote Computer โดยคัดลอกจาก Current หรือ Update จาก PSGallery)
  • Reset-WUComponents – Reset Windows Update Agent บนคอมพิวเตอร์กลับสู่ Default State

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ตรวจสอบ Windows Update Client Settings ปัจจุบัน:

Get-WUSettings

ComputerName : WKS5S2N39S2 WUServer : https://MN-WSUS:8530 WUStatusServer : https://MN-WSUS:8530 AcceptTrustedPublisherCerts : 1 ElevateNonAdmins : 1 DoNotConnectToWindowsUpdateInternetLocations : 1 TargetGroupEnabled : 1 TargetGroup : ServersProd NoAutoUpdate : 0 AUOptions : 3 - Notify before installation ScheduledInstallDay : 0 - Every Day ScheduledInstallTime : 3 UseWUServer : 1 AutoInstallMinorUpdates : 0 AlwaysAutoRebootAtScheduledTime : 0 DetectionFrequencyEnabled : 1 DetectionFrequency : 4

ในตัวอย่าง Windows Update Agent ตั้งค่าด้วย GPO รับ Updates จาก Local WSUS Server

Cmdlet Reset-WUComponents -Verbose รีเซ็ต Windows Update Agent Settings ทั้งหมด Re-register Libraries และ Restore Service wususerv สู่ Default State

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

สแกนและดาวน์โหลด Windows Updates ด้วย PowerShell

รายการ Updates ที่มีสำหรับคอมพิวเตอร์ปัจจุบันบน Update Server ด้วย Get-WindowsUpdate หรือ Get-WUList

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ตรวจสอบรายการ Updates บน Remote Computer:

Get-WUList –ComputerName server2

ตรวจสอบว่า Windows ควรรับ Updates จากที่ไหน รัน:

Get-WUServiceManager

ServiceID IsManaged IsDefault Name --------- --------- --------- ---- 8b24b027-1dee-babb-9a95-3517dfb9c552 False False DCat Flighting Prod 855e8a7c-ecb4-4ca3-b045-1dfa50104289 False False Windows Store (DCat Prod) 3da21691-e39d-4da6-8a4b-b43877bcb1b7 True True Windows Server Update Service 9482f4b4-e343-43b6-b170-9a65bc822c77 False False Windows Update

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

เห็นว่าคอมพิวเตอร์ตั้งค่ารับ Updates จาก Local WSUS Server (Windows Server Update Service = True) ควรเห็นรายการ Updates ที่ Approved ให้คอมพิวเตอร์

หากต้องการสแกนกับ Microsoft Update Servers บน Internet (นอกจาก Windows Updates Server นี้ยังมี Office และ Microsoft Product Updates อื่นๆ) รัน:

Get-WUlist -MicrosoftUpdate

จะได้ Warning:

Get-WUlist : Service Windows Update was not found on computer. Use Get-WUServiceManager to get registered service.

เพื่ออนุญาต Scanning บน Microsoft Update รัน:

Add-WUServiceManager -ServiceID "7971f918-a847-4430-9279-4a52d1efe18d" -AddServiceFlag 7

ตอนนี้สแกนกับ Microsoft Update ได้ ในกรณีนี้พบ Updates เพิ่มเติมสำหรับ Microsoft Visual C++ 2008 และ Microsoft Silverlight

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ตรวจสอบ Windows Update Agent Version:

Get-WUApiVersion

ComputerName PSWindowsUpdate PSWUModuleDll ApiVersion WuapiDllVersion ------------ --------------- ------------- ---------- --------------- DESKTOP-J... 2.1.1.2 2.2.0.2 8.0 10.0.19041.1320

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ลบ Products หรือ KBs เฉพาะจากรายการ Updates ที่คอมพิวเตอร์รับได้ โดย Exclude ด้วย:

  • Category (-NotCategory)
  • Title (-NotCategory)
  • Update Number (-NotKBArticleID)

ตัวอย่าง Exclude OneDrive, Driver Updates และ KB เฉพาะ:

Get-WUlist -NotCategory "Drivers" -NotTitle "OneDrive" -NotKBArticleID KB4489873

ติดตั้ง Windows Updates ด้วย PowerShell (Install-WindowsUpdate)

Download และ Install Updates ทั้งหมดที่มีสำหรับ Windows Device จาก Windows Update Servers (แทน Local WSUS) รัน:

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot

Parameter AcceptAll ยอมรับการติดตั้ง Update Packages ทั้งหมด AutoReboot อนุญาตให้ Windows Restart อัตโนมัติหลังติดตั้ง Updates

ใช้ Options เพิ่มเติมได้:

  • IgnoreReboot – ปิด Auto Reboot
  • ScheduleReboot – ตั้งเวลา Restart Computer แบบแน่นอน

บันทึก Update Installation History ไป Log File (ใช้แทน WindowsUpdate.log File)

Install-WindowsUpdate -AcceptAll -Install -AutoReboot | Out-File "c:\logs\$(get-date -f yyyy-MM-dd)-WindowsUpdate.log" -force

ติดตั้งเฉพาะ Update Packages ด้วย KB Numbers:

Get-WindowsUpdate -KBArticleID KB2267602, KB4533002 -Install

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ในกรณีนี้ต้อง Confirm การติดตั้งแต่ละ Update ด้วยตนเอง

หากต้องการ Exclude Updates เฉพาะจาก Installation List รัน:

Install-WindowsUpdate -NotCategory "Drivers" -NotTitle OneDrive -NotKBArticleID KB4011670 -AcceptAll -IgnoreReboot

ติดตั้ง Windows Update บน Remote Computers ด้วย PowerShell

PSWindowsUpdate Module อนุญาตติดตั้ง Updates แบบ Remote บน Workstations หรือ Servers หลายเครื่องพร้อมกัน (PSWindowsUpdate ต้องติดตั้ง/Import บน Computers นั้นๆ) สะดวกมากเพราะ Admin ไม่ต้อง Login เข้า Remote Windows Hosts ด้วยตนเอง WinRM ต้อง Enable และ Configure บน Remote Computers (Manual หรือผ่าน GPO)

เกือบทุก Cmdlet ใน PSWindowsUpdate Module จัดการและติดตั้ง Windows Updates บน Remote Computers ด้วย Attribute –Computername

ติดตั้ง PSWindowsUpdate Module บน Remote Computers และอนุญาต Access ผ่าน Dynamic RPC Ports ไปยัง Process dllhost.exe ใน Windows Defender Firewall ใช้ Invoke-Command ตั้งค่า PSWindowsUpdate บน Remote Computers:

$Targets = "lon-fs02", "lon-db01"
Invoke-Command -ComputerName $Target -ScriptBlock {Set-ExecutionPolicy RemoteSigned -force }
Invoke-Command -ComputerName $Target -ScriptBlock {Import-Module PSWindowsUpdate; Enable-WURemoting}

PSWindowsUpdate Module ใช้จัดการ Windows Updates แบบ Remote ทั้งใน AD Domain และ Workgroup (ต้องตั้งค่า PowerShell Remoting (WinRM) สำหรับ Workgroup Environment)

เพื่อจัดการ Updates บน Remote Computers ต้องเพิ่ม Hostnames ใน WinRM Trusted Host List หรือ Configure PowerShell Remoting (WinRM) ผ่าน HTTPS:

winrm set winrm/config/client '@{TrustedHosts="server1,server2,…"}'

หรือ PowerShell: Set-Item wsman:\localhost\client\TrustedHosts -Value server1 -Force

คำสั่งต่อไปติดตั้ง Updates ทั้งหมดบน Remote Windows Hosts 3 เครื่อง:

$ServerNames = "server1, server2, server3"
Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate -AcceptAll | Out-File C:\Windows\PSWindowsUpdate.log } -RunNow -Confirm:$false -Verbose -ErrorAction Ignore

Invoke-WUJob Cmdlet (ชื่อก่อนหน้า Invoke-WUInstall) สร้าง Scheduler Task บน Remote Computer รันภายใต้ Local SYSTEM Account

ระบุเวลาที่แน่นอนติดตั้ง Windows Updates:

Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate –AcceptAll -AutoReboot | Out-File C:\Windows\PSWindowsUpdate.log } -Confirm:$false -TriggerDate (Get-Date -Hour 22 -Minute 0 -Second 0)

ตรวจสอบ Status ของ Update Installation Task ด้วย Get-WUJob:

Get-WUJob -ComputerName $ServerNames

หากคืนค่า Empty List แสดงว่า Update Installation Task บนทุก Computer เสร็จแล้ว

ติดตั้ง Updates บน Remote Computer และส่ง Email Report ไปยัง Admin:

Install-WindowsUpdate -ComputerName nysrv1 -MicrosoftUpdate -AcceptAll - IgnoreReboot -SendReport –PSWUSettings @{SmtpServer="smtp.woshub.com";From="update_alert@woshub.com";To="wsus_admin@woshub.com";Port=25} -Verbose

ตรวจสอบ Windows Update History ด้วย PowerShell (Get-WUHistory)

ใช้ Cmdlet Get-WUHistory ดูรายการ Updates ที่ติดตั้งบนคอมพิวเตอร์ก่อนหน้านี้ แบบ Auto หรือ Manual

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ดูวันที่ติดตั้ง Update เฉพาะ:

Get-WUHistory| Where-Object {$_.Title -match "KB4517389"} | Select-Object *|ft

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ตรวจสอบว่า Update เฉพาะติดตั้งบน Remote Computers หลายเครื่องหรือไม่:

"server1","server2" | Get-WUHistory| Where-Object {$_.Title -match "KB4011634"} | Select-Object *|ft

ตรวจสอบว่าคอมพิวเตอร์ต้อง Restart หลังติดตั้ง Update (Pending Reboot):

Get-WURebootStatus –ComputerName WKS21TJS

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ดูค่า Attribute RebootRequired และ RebootScheduled

สร้าง Report พร้อมวันที่ติดตั้ง Updates ล่าสุดบน Computers ทั้งหมดใน Domain ด้วย Get-ADComputer Cmdlet (จาก Active Directory PowerShell Module):

$Computers=Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows*' }
Foreach ($Computer in $Computers)
{
Get-WULastResults -ComputerName $Computer.Name|select ComputerName, LastSearchSuccessDate, LastInstallationSuccessDate
}

แบบเดียวกัน หา Computers ที่ ไม่ได้ติดตั้ง Updates มานานกว่า 60 วัน แสดงผลใน Out-GridView Interactive Table:

$result=@()
Foreach ($Computer in $Computers) {
$result+= Get-WULastResults -ComputerName $Computer.Name
}
$result| Where-Object { $_.LastInstallationSuccessDate -lt ((Get-Date).AddDays(-60)) }| Out-GridView

Uninstall Windows Updates ด้วย PowerShell (Remove-WindowsUpdate)

ใช้ Cmdlet Remove-WindowsUpdate Uninstall Updates ด้วย PowerShell อย่างถูกต้อง ระบุ KB Number เป็น Argument ของ Parameter KBArticleID เพื่อหน่วง Auto Restart เพิ่ม Option –NoRestart:

Remove-WindowsUpdate -KBArticleID KB4489873 -NoRestart

วิธีซ่อน Windows Updates ด้วย PowerShell

ซ่อน Updates เฉพาะ เพื่อไม่ให้ Windows Update Service ติดตั้งบนคอมพิวเตอร์ (ส่วนใหญ่ซ่อน Driver Updates) ตัวอย่าง ซ่อน KB4489873 และ KB4489243:

$HideList = "KB4489873", "KB4489243"
Get-WindowsUpdate -KBArticleID $HideList –Hide

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

ครั้งต่อไปที่สแกน Updates ด้วย Get-WUlist Hidden Updates จะไม่แสดงในรายการ Updates ที่พร้อมติดตั้ง

แสดงรายการ Updates ที่ซ่อนบน Computer นี้:

Get-WindowsUpdate –IsHidden

สังเกตว่า Attribute H (Hidden) ปรากฏใน Column Status ของ Hidden Updates

จัดการ Windows Updates ด้วยโมดูล PSWindowsUpdate PowerShell

Unhide Updates บางตัว รัน:

Get-WindowsUpdate -KBArticleID $HideList -WithHidden -Hide:$false

หรือ: Show-WindowsUpdate -KBArticleID $HideList

สำหรับผู้ที่ไม่สะดวกกับ PowerShell Console แนะนำ Graphic Windows Update MiniTool จัดการ Updates ใน Windows 10/11 และ Windows Server 2022/2019