When are Windows patches happening?

As most people know, Microsoft releases patches on the second Tuesday of the month. In some organizations these patches are deployed to development, test and QA systems within a couple of days of the release. However, production systems can’t be patched until the next maintenance window. Assuming this patch window is on a Saturday, we need to find the first Saturday after the second Tuesday. When looking at a calendar this is pretty simple for most people to figure out. However, when you need a whole years worth it takes a few minutes to calculate. What we need is some code to look these up for us…like this function:

[cc lang=”Powershell”]
Function Get-WsusSaturday {
param([datetime]$date=(Get-Date) )
$numberOfTuesdays=0

1..([datetime]::DaysInMonth( $date.year , $date.month )) | %{
$thisDate = Get-Date “$($date.month)/$($_)/$($date.year)”
if ([string]$thisDate.DayOfWeek -eq ‘Tuesday’) {
$numberOfTuesdays++
if ($numberOfTuesdays -eq 2) {
$thisDate.AddDays(4).ToLongDateString()
}
}
}
}
[/cc]
Line 2: accept the date as a parameter and cast it to a date/time value, defaulting to today’s date if nothing is provided
Line 3: max sure we don’t think its Tuesday already
Line 5: starting on the first of each month, loop through the max number of days in the month
Line 8: if the date is Tuesday, add one to the counter
Line 9: once we find the second Tuesday
Line 10: add 4 more days to find the following Saturday and return the date text

You can take this function and do something creative, like find the next 5 years worth of patch Saturdays.
[cc lang=”powershell”]
13..18 | %{
$year = $_
1..12 | %{ Get-WsusSaturday “$_/1/$year” }
}
[/cc]

This entry was posted in Scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.