用于检查 Windows 更新状态的 PowerShell 脚本
通常,希望了解其 Windows 10 系统上是否安装了最新累积更新的用户使用此方法来检查 Windows 10 更新历史记录。在这篇文章中,我们将向您展示如何使用 PowerShell 脚本获取 Windows 10 的当前补丁信息。(how to get current patch information for Windows 10 using a PowerShell script.)
(PowerShell)用于检查Windows 更新(Windows Update)状态的PowerShell脚本
PowerShell脚本可用于报告Windows 10计算机当前正在哪个操作系统构建,以及哪个更新是设备可用的最新更新。它还可以报告为工作站当前运行的Windows 10版本发布的所有(Windows 10)Windows更新。
运行脚本时,将显示以下信息:
- 当前操作系统版本
- 当前操作系统版本
- 当前操作系统内部版本号
- 与该内部版本号相对应的已安装更新,以及 KB 号和指向信息页面的链接
- 操作系统版本的最新可用更新
要使用PowerShell脚本获取(PowerShell)Windows 10当前补丁信息,您需要使用来自Github的以下代码(Github)创建并运行 PowerShell 脚本(create and run the PowerShell script)。
[CmdletBinding()] Param( [switch]$ListAllAvailable, [switch]$ExcludePreview, [switch]$ExcludeOutofBand ) $ProgressPreference = 'SilentlyContinue' $URI = "https://aka.ms/WindowsUpdateHistory" # Windows 10 release history Function Get-MyWindowsVersion { [CmdletBinding()] Param ( $ComputerName = $env:COMPUTERNAME ) $Table = New-Object System.Data.DataTable $Table.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build")) $ProductName = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ProductName).ProductName Try { $Version = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name ReleaseID -ErrorAction Stop).ReleaseID } Catch { $Version = "N/A" } $CurrentBuild = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name CurrentBuild).CurrentBuild $UBR = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name UBR).UBR $OSVersion = $CurrentBuild + "." + $UBR $TempTable = New-Object System.Data.DataTable $TempTable.Columns.AddRange(@("ComputerName","Windows Edition","Version","OS Build")) [void]$TempTable.Rows.Add($env:COMPUTERNAME,$ProductName,$Version,$OSVersion) Return $TempTable } Function Convert-ParsedArray { Param($Array) $ArrayList = New-Object System.Collections.ArrayList foreach ($item in $Array) { [void]$ArrayList.Add([PSCustomObject]@{ Update = $item.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - ') KB = "KB" + $item.href.Split('/')[-1] InfoURL = "https://support.microsoft.com" + $item.href OSBuild = $item.outerHTML.Split('(OS ')[1].Split()[1] # Just for sorting }) } Return $ArrayList } If ($PSVersionTable.PSVersion.Major -ge 6) { $Response = Invoke-WebRequest -Uri $URI -ErrorAction Stop } else { $Response = Invoke-WebRequest -Uri $URI -UseBasicParsing -ErrorAction Stop } If (!($Response.Links)) { throw "Response was not parsed as HTML"} $VersionDataRaw = $Response.Links | where {$_.outerHTML -match "supLeftNavLink" -and $_.outerHTML -match "KB"} $CurrentWindowsVersion = Get-MyWindowsVersion -ErrorAction Stop If ($ListAllAvailable) { If ($ExcludePreview -and $ExcludeOutofBand) { $AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Preview" -and $_.outerHTML -notmatch "Out-of-band"} } ElseIf ($ExcludePreview) { $AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Preview"} } ElseIf ($ExcludeOutofBand) { $AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Out-of-band"} } Else { $AllAvailable = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0]} } $UniqueList = (Convert-ParsedArray -Array $AllAvailable) | Sort OSBuild -Descending -Unique $Table = New-Object System.Data.DataTable [void]$Table.Columns.AddRange(@('Update','KB','InfoURL')) foreach ($Update in $UniqueList) { [void]$Table.Rows.Add( $Update.Update, $Update.KB, $Update.InfoURL ) } Return $Table } $CurrentPatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'} | Select -First 1 If ($ExcludePreview -and $ExcludeOutofBand) { $LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Out-of-band" -and $_.outerHTML -notmatch "Preview"} | Select -First 1 } ElseIf ($ExcludePreview) { $LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Preview"} | Select -First 1 } ElseIf ($ExcludeOutofBand) { $LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0] -and $_.outerHTML -notmatch "Out-of-band"} | Select -First 1 } Else { $LatestAvailablePatch = $VersionDataRaw | where {$_.outerHTML -match $CurrentWindowsVersion.'OS Build'.Split('.')[0]} | Select -First 1 } $Table = New-Object System.Data.DataTable [void]$Table.Columns.AddRange(@('OSVersion','OSEdition','OSBuild','CurrentInstalledUpdate','CurrentInstalledUpdateKB','CurrentInstalledUpdateInfoURL','LatestAvailableUpdate','LastestAvailableUpdateKB','LastestAvailableUpdateInfoURL')) [void]$Table.Rows.Add( $CurrentWindowsVersion.Version, $CurrentWindowsVersion.'Windows Edition', $CurrentWindowsVersion.'OS Build', $CurrentPatch.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - '), "KB" + $CurrentPatch.href.Split('/')[-1], "https://support.microsoft.com" + $CurrentPatch.href, $LatestAvailablePatch.outerHTML.Split('>')[1].Replace('</a','').Replace('—',' - '), "KB" + $LatestAvailablePatch.href.Split('/')[-1], "https://support.microsoft.com" + $LatestAvailablePatch.href ) Return $Table
您可以排除比您安装的更新更新的可用预览(Preview)或带外更新被报告为最新的可用更新,因此您可以通过运行以下命令专注于累积更新:(Out-of-band)
Get-CurrentPatchInfo -ExcludePreview -ExcludeOutofBand
您还可以使用以下命令列出Microsoft为您的操作系统版本发布的所有Windows更新:
Get-CurrentPatchInfo -ListAvailable
如果您想从列表中排除预览(Preview)版和带外更新,但要列出(Out-of-band)Microsoft为您的操作系统版本发布的所有Windows更新,请运行以下命令:
Get-CurrentPatchInfo -ListAvailable -ExcludePreview -ExcludeOutofBand
就是这样!
阅读下一篇(Read next):PowerShell 模块浏览器站点(PowerShell Module Browser site)可让您搜索 cmdlet 和包。
Related posts
Reset Windows Update Client使用PowerShell Script
Windows Update page上的Fix问题按钮
最佳实践,以提高Windows Update installation倍
在哪里找到以及如何在Windows 11/10中读取Windows Update log
如何修复Windows Update error 0x80240061
Fix Windows Update error 0x80070659
无法使用error code 0x8024200D安装Windows Update
如何在Windows 10中暂停Windows Update,最长365天
Windows 10上的Windows Update error 80072EFE
Fix Windows Update error 0x80240439在Windows 10上
如何修复Windows Update Error Code 0x80070012
Windows Update误差0x800705b4,0x8024402f,0x80070422 [Fixed}
Fix的Windows 10 Update Error 0x800703F1
Windows Update失败,Error Code 0x8024004a安装
如何在Windows 10关闭自动Windows Update
Fix Windows Update Error 0x80070005
Windows Update未能安装或不会在Windows 11/10下载
如何Fix Windows Update Error Code 80244010
Fix Windows Update Error 0x800f0989在Windows 11/10上
Windows 10 Win Update Stop:禁用Windows Updates4