Get-PatchTuesday.ps1


<#PSScriptInfo
 
    .VERSION 1.0.0
 
    .GUID 58112e76-6000-484d-ace5-7f6746c33067
 
    .AUTHOR G. Dees
 
    .COMPANYNAME
 
    .COPYRIGHT
 
    .TAGS Patch Update Tuesday
 
    .LICENSEURI
 
    .PROJECTURI
 
    .ICONURI
 
    .EXTERNALMODULEDEPENDENCIES
 
    .REQUIREDSCRIPTS
 
    .EXTERNALSCRIPTDEPENDENCIES
 
    .RELEASENOTES
        1.0.0 - Initial release
 
    .PRIVATEDATA
 
#>


# function Get-PatchTuesday {

    <#
        .SYNOPSIS
            Gets the date and time of the next Patch Tuesday.
 
        .DESCRIPTION
            The Get-PatchTuesday cmdlet gets a DateTime object that represents the next
            Patch Tuesday or the Patch Tuesday of the year and month that you specify.
 
            Patch Tuesday occurs on the second, and sometimes fourth, Tuesday of each month.
 
        .PARAMETER Year
            Specifies the year that is displayed. Enter a value from 1 to 9999.
 
        .PARAMETER Month
            Specifies the month that is displayed. Enter a value from 1 to 12.
 
        .EXAMPLE
            (Get-PatchTuesday) - [datetime]::Now
 
            Gets the amount of time until the next Patch Tuesday.
    #>


    [CmdletBinding(DefaultParameterSetName = 'Month')]

    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'Year')]
        [ValidateRange(1, 9999)]
        [int]$Year = [datetime]::Now.Year,

        [Parameter(ParameterSetName = 'Month', Position = 0)]
        [Parameter(Mandatory = $true, ParameterSetName = 'Year')]
        [ValidateRange(1, 12)]
        [int]$Month = [datetime]::Now.Month
    )

    $Date = [datetime]::new($Year, $Month, 1)


    while ($Date.DayOfWeek -ne 'Tuesday') { $Date = $Date.AddDays(1) }  # First Tuesday

    if (($PSBoundParameters.Count -eq 0) -and ([datetime]::Now -gt $Date.AddDays(7))) {
        if ($Date.Month -lt 12) { $parameters = @{ Month = $Date.Month + 1 } }
        else { $parameters = @{ Year = $Date.Year + 1 ; Month = 1 } }

        Get-PatchTuesday @parameters  # Go to the next month
    }

    else { $Date.AddDays(7) }  # Second Tuesday
# }