Created
April 8, 2021 23:08
-
-
Save instance-id/ca12d184c0bab2b35a5da2b69a100c5c to your computer and use it in GitHub Desktop.
Return the most recent 'Patch Tuesday' date in YYYY-MM-DD format
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
#region --------------------------------------- Get-PatchTuesday | |
# --- Modified from: https://github.com/tsrob50/Get-PatchTuesday | |
# -------------------------------------------------------------- | |
Function Get-PatchTuesday { | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(position = 0)] | |
[ValidateSet('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')] | |
[String]$weekDay = 'Tuesday', | |
[ValidateRange(0, 5)] | |
[Parameter(position = 1)] | |
[int]$findNthDay = 2 | |
) | |
[datetime]$today = [datetime]::NOW | |
function Calculate($date) { | |
$dateM = $date.Month.ToString() | |
$dateY = $date.Year.ToString() | |
[datetime]$strtMonth = $dateM + '/1/' + $dateY | |
while ($strtMonth.DayofWeek -ine $weekDay) { $strtMonth = $StrtMonth.AddDays(1) } | |
$firstWeekDay = $strtMonth | |
# -- Identify and calculate the day offset | |
if ($findNthDay -eq 1) { $dayOffset = 0 } else { $dayOffset = ($findNthDay - 1) * 7 } | |
return $firstWeekDay.AddDays($dayOffset) | |
} | |
# -- Determine patch tuesday for this current month -------- | |
[datetime] $patchTuesday = Calculate $today | |
if ($today -lt $patchTuesday) { | |
return $(Calculate $today.AddMonths(-1)).ToString('yyyy-MM-dd') | |
} | |
else { return $patchTuesday.ToString('yyyy-MM-dd') } | |
} | |
#endregion ----------------------------------------------------- | |
# ------------------------------------------------ Usage Example | |
# -------------------------------------------------------------- | |
$mostRecentPatchTuesday = Get-PatchTuesday | |
Write-Host $mostRecentPatchTuesday # 2021-03-09 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment