For fun, here's what I came up with awhile ago to find Patch Tuesday:
function Get-MicrosoftPatchDay
{
[CmdletBinding()]
param([Parameter(Mandatory=$False)] [int] $month = -1)
$now = Get-Date
if ($month -eq -1) { $month = $now.Month }
# BUG: this uses the current minute, hour, second to construct the date.
# keep that in mind for comparison (worse), or set it to a specific time (safer)
$startofmonth = Get-Date -Year $now.Year -Month $month -Day 1
$tempdate = $startofmonth
$tuesdays = 0
if ($tempdate.DayOfWeek -eq "Tuesday") {$tuesdays++}
while ($tuesdays -lt 2)
{
$tempdate = $tempdate.AddDays(1)
if ($tempdate.DayofWeek -eq "Tuesday") { $tuesdays++ }
}
$tempdate
}
Rewrote this because it was bothering me that the tuesday thing was hardcoded:
# This helper function will find the $nth $day of a certain month.
# For instance, the second Tuesday for Microsoft Patch Day
# If no month or year are passed in, we use the current month and/or year
# Return $null if there is no valid answer (for example the 8th Tuesday will not be in the current month)
function Get-NthDayOfMonth
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$True)] [string] $dayname, # The Day of Week we will be counting (ex: Monday)
[Parameter(Mandatory=$True)] [int] $nthday, # The nth $dayname (ex: 2 means second $dayname)
[Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month, # The month we will be using for our constructed date
[Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year # The year we will be using for our constructed date
)
# TODO: validate $dayname as a valid dayname
$numfound = 0
$tempdate = $null
# Construct the date object for the month we'll be examining
try
{
$tempdate = Get-Date -Year $year -month $month -Day 1
if ($tempdate.DayOfWeek -eq $dayname) { $numfound++ }
while (($numfound -lt $nthday) -and ($tempdate.month -eq $month))
{
$tempdate = $tempdate.AddDays(1)
if ($tempdate.DayOfWeek -eq $dayname) { $numfound++ }
}
}
catch { Write-Output "Invalid date." }
# Verify that we found the right number (prevent going into next month)
if ($numfound -ne $nthday) { $tempdate = $null }
$tempdate
}
function Get-MicrosoftPatchDay
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month,
[Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year
)
Get-NthDayOfMonth -dayname "Tuesday" -nthday 2 -month $month -year $year
}
function Get-HappysysadmPatchDay
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)] [int] $month = (Get-Date).Month,
[Parameter(Mandatory=$False)] [int] $year = (Get-Date).Year
)
Get-NthDayOfMonth -dayname "Monday" -nthday 4 -month $month -year $year
}
Get-NthDayOfMonth -dayname "Monday" -nthday 8
Get-NthDayOfMonth -dayname "Wednesday" -nthday 3 -year 1995
Get-NthDayOfMonth -dayname "Tuesday" -nthday 2 -month 4
Get-NthDayOfMonth -dayname "Friday" -nthday 3 -month 9 -year 2012
Get-MicrosoftPatchDay
Get-HappysysadmPatchDay
3
u/Beablebeable Jun 07 '18 edited Jun 07 '18
Nice article, thank you.
For fun, here's what I came up with awhile ago to find Patch Tuesday: