Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Find-WindowsUpdateLastCUInstalled {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $false)]
- [Switch]
- $ByHistory
- )
- begin {
- $OSVer = (Get-Item C:\Windows\System32\ntoskrnl.exe).VersionInfo.FileVersionRaw
- if ($OSVer.Major -ne 10 -or $OSVer.Minor -ne 0)
- { throw [System.NotSupportedException]::new("OS version must be 10.0.x.x") }
- }process {
- if ($ByHistory) {
- Write-PSFMessage -Level Warning -Message "This method is less reliable as uninstallations are not included in the history."
- $UpdateSession = (New-Object -ComObject 'Microsoft.Update.Session')
- $UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
- $UpdateHistory = $UpdateSearcher.QueryHistory(0, $UpdateSearcher.GetTotalHistoryCount())
- $LastCU = $UpdateHistory | Where-Object {
- $_.Title -match "\d{4}-\d{2}\sCumulative Update for (?:Windows|Microsoft\sserver\soperating\ssystem)" -and $_.ResultCode -eq 2 } |
- Sort-Object Date -Descending | Select-Object -First 1
- if ($LastCU) {
- [void]($LastCU.Title -match "(?<Series>\d{4}-\d{2})\sCumulative Update for (?:Windows|Microsoft\sserver\soperating\ssystem)")
- $LastCU | Add-Member -NotePropertyName "Series" -NotePropertyValue ([DateTime]::Parse($Matches.Series)) -Force
- return $LastCU
- }
- }
- else {
- if (-not (Test-Path -Path "$PSScriptRoot\BuildTable.json")) {
- Write-PSFMessage -Level Error -Message "Please run 'Update-WindowsUpdateCUBuildTable' before running this command. Please note, you will need an Internet connection for this command."
- return
- }
- elseif (((Get-Date) - (Get-Item -Path "$PSScriptRoot\BuildTable.json").LastWriteTime).TotalDays -ge 30) {
- Write-PSFMessage -Level Warning -Message "It has been at least 30 days since the build table was last updated. It is recommended to run 'Update-WindowsUpdateCUBuildTable' (requires an Internet connection)."
- }
- try
- { $BuildTable = Get-Content -Path "$PSScriptRoot\BuildTable.json" -Raw | ConvertFrom-Json -ErrorAction Stop }
- catch
- { Write-PSFMessage -Level Critical -Message "The build table seems to be corrupted. Please run 'Update-WindowsUpdateCUBuildTable' (requires an Internet connection)."; return }
- if (-not ($BuildTable | Get-Member -MemberType NoteProperty -Name "Build"))
- { Write-PSFMessage -Level Critical -Message "The build table seems to be corrupted. Please run 'Update-WindowsUpdateCUBuildTable' (requires an Internet connection)."; return }
- return $BuildTable | Where-Object { $_.Build.Major -eq $OSVer.Major -and $_.Build.Minor -eq $OSVer.Minor -and $_.Build.Revision -eq $OSVer.Revision } | Select-Object -First 1
- }
- }
- }
- function Update-WindowsUpdateCUBuildTable {
- [CmdletBinding()]
- param ()
- begin {
- $WIN_MAJORMINOR = "10.0"
- $WIN_KB_URLS = @(
- "https://support.microsoft.com/en-us/help/5018682", # Windows 10, Server 2016, Server 2019
- "https://support.microsoft.com/en-us/help/5031682" # Windows 11
- "https://support.microsoft.com/en-us/help/5005454" # Server 2022
- )
- $NAVTAB_REGEX = "(?i)((?:(?:Jan|Feb|Mar|Apr|May|June|July|Aug|Sept|Oct|Nov|Dec)\.{0,1}|(?:January|February|March|April|May|June|July|August|September|October|November|December))\s+\d{1,2},\s+\d{4}).+(KB\d+){1}.+(?:OS Build(?:s){0,1}\s((?:\d+\.\d+).*))\)(?:\s(.+)<)*"
- $BuildTable = @()
- }process {
- $WIN_KB_URLS | ForEach-Object {
- try
- { $htmlSrc = Invoke-RestMethod -Uri $_ -UseBasicParsing -ErrorAction Stop }
- catch
- { Write-PSFMessage -Level Critical -Message $Error[0]; return }
- [Regex]::Matches($htmlSrc, $NAVTAB_REGEX) | ForEach-Object {
- $g = $_.Groups
- ($g[3] -replace "[^0-9. ]", "").Split(" ") | Where-Object { $_ } | ForEach-Object {
- $BuildTable += [PSCustomObject]@{
- KB = $g[2].Value
- DateStamp = [DateTime]::Parse($g[1].Value)
- Build = [Version]"$WIN_MAJORMINOR.$_"
- Descriptor = $g[4].Value
- }
- }
- }
- }
- $BuildTable | ConvertTo-Json | Out-File -FilePath "$PSScriptRoot\BuildTable.json"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement