Last active
March 3, 2017 06:03
-
-
Save kking124/8a6fb25dedd76f0c8a31bdfa404ae7b8 to your computer and use it in GitHub Desktop.
Check to see if required rollup has been installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
check-rollup.ps1 | |
Author: kking124 (https://github.com/kking124) | |
Version: 1.0.3 | |
Copyright 2017 kking124 | |
LicenseL MIT (https://opensource.org/licenses/MIT) | |
#> | |
function Check-Rollup { | |
param ([Parameter(Mandatory=$true)][string] $Id, [switch] $Is32, [switch] $V2) | |
<# | |
.SYNOPSIS | |
Tries to check to see if a Windows Update Rollup has been applied - requires interwebs and admin privs | |
.DESCRIPTION | |
TBD | |
.PARAMETER Id | |
Required; Specifies the KB to search for | |
.PARAMETER Is32 | |
Optional; Detect 32 bit patch dates instead of 64 bit patch dates | |
.PARAMETER V2 | |
Optional; Force using Powershell V2 compatible commands | |
.Example | |
C:\PS> Check-Rollup -Id KB3201845 | |
Checks to see if the KB3201845 is installed on the machine | |
On Windows 10 this MAY return True | |
On all other systems this WILL return False | |
.Notes | |
Currently does not work with Powershell V2 | |
#> | |
[System.DateTime] $rollupDate = New-Object System.DateTime | |
[System.DateTime] $currentRollupDate = New-Object System.DateTime | |
function get-html{ | |
param([string] $uri) | |
#TODO: add v2 compatability | |
return Invoke-WebRequest $uri | |
} | |
if(!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
throw (new-object System.Management.Automation.PSSecurityException); | |
} | |
if(!$PSBoundParameters.ContainsKey('V2') -or -not $V2) { | |
$V2 = $PSVersionTable.PSVersion.Version -lt 4 | |
} | |
$hotfix = Get-Hotfix $Id -ErrorAction SilentlyContinue | |
if($hotfix -ne $null) { | |
return $true | |
} | |
$webRequest = get-html "https://www.catalog.update.microsoft.com/Search.aspx?q=$Id" | |
if($webRequest.ParsedHtml.getElementById('ctl00_catalogBody_noResults') -ne $null) { | |
return $false; | |
} | |
if(-not [System.DateTime]::TryParse($webRequest.ParsedHtml.getElementById('tableContainer').getElementsByTagName('tr')[2].getElementsByTagName('td')[4].InnerHtml, [ref] $rollupDate)) { | |
throw(New-Object System.Exception "$Id was not found at https://www.catalog.update.microsoft.com") | |
return $false | |
} | |
#based on: https://p0w3rsh3ll.wordpress.com/2012/10/25/getting-windows-updates-installation-history/ | |
$Session = New-Object -ComObject Microsoft.Update.Session | |
$Searcher = $Session.CreateUpdateSearcher() | |
$hotfixList = $Searcher.QueryHistory(0,$HistoryCount) | | |
Where-Object { $_.Title -ne $null -and $_.Title.contains('Cumulative Update') -and ($Is32 -or $_.Title.contains('x64')) } | | |
ForEach-Object { | |
New-Object -TypeName PSObject -Property @{ | |
Title = ($_.Title -split '.*(KB\d{6,7}).*')[1]; | |
InstalledOn = Get-Date -Date $_.Date; | |
} | |
} | Sort-Object -Descending:$true -Property InstalledOn | | |
ForEach-Object { $_.Title } | |
if($hotfixList.Count -eq 0) { | |
throw(New-Object System.Exception "No Windows Update Rollups Installed") | |
return $false | |
} | |
$hotfixList | ForEach-Object { | |
$webRequest = get-html "https://www.catalog.update.microsoft.com/Search.aspx?q=$_" | |
[System.DateTime] $date = New-Object System.DateTime | |
#quit early for errors | |
if($webRequest.ParsedHtml.getElementById('ctl00_catalogBody_noResults') -ne $null) { | |
continue; | |
} | |
if([System.DateTime]::TryParse($webRequest.ParsedHtml.getElementById('tableContainer').getElementsByTagName('tr')[2].getElementsByTagName('td')[4].InnerHtml, [ref] $date)) { | |
if([System.DateTime]::Compare($date,$currentRollupDate) -gt 0) { | |
$currentRollupDate = $date | |
} | |
} | |
} | |
if(([System.DateTime]::Compare( $currentRollupDate, (New-Object System.DateTime) )) -ne 0) { | |
throw(New-Object System.Exception "No Windows Update Rollup Update Dates Found") | |
return $false | |
} | |
return [System.DateTime]::Compare($currentRollupDate,$rollupDate) -ge 0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
check-rollup.tests.ps1 | |
Author: kking124 (https://github.com/kking124) | |
Version: 1.0.0 | |
Copyright 2017 kking124 | |
LicenseL MIT (https://opensource.org/licenses/MIT) | |
#> | |
function Test-CheckRollup { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
License
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Changelog
1.0.3 (2017-02-24)
Fixed but at line 91 that caused detected rollups to throw an error instead of success
Added more description and moved into the function so that Get-Help can use it
Updated the case on variables to be consistent with PowerShell
Set up get-html to augment
Invoke-WebRequest
Added more License details
Made Id mandatory
1.0.2 (2017-02-24)
Added some error checking
1.0.1 (2017-02-24)
Use different Administrator check
1.0 (2017-02-24)
Initial Build
Notes
Requires Powershell 4.0
TODO
Invoke-WebRequest
withSystem.Net.HttpWebRequest
for v2 compatability