Functions/Data/Get-DayofMonth.ps1

Function Get-DayofMonth
    {
    [cmdletbinding()]
    Param
        (
        # Ordinal of Weekday
        [Parameter(Mandatory=$True)]
        [ValidateSet("1st","2nd","3rd","4th","5th","Last")]
        [string]
        $Ordinal,

        # Weekday
        [Parameter(Mandatory=$true)]
        [ValidateSet("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")]
        [string]
        $Weekday,

        # Time to Inject in 24hour time
        [Parameter(Mandatory=$false)]
        [string]
        $InjectTime = "00:00",

        # Month
        [Parameter(Mandatory=$false)]
        [string]
        $Month = ((get-date).month),

        # Year
        [Parameter(Mandatory=$false)]
        [string]
        $Year = ((get-date).year)
        )

    Process
        {
        # Get First day of the Month
        $FirstDay = [datetime]([string]$month + "/1/" + [string]$Year)

        # Get Occurences of Weekday of Month
        $Occurrences = (0..31 | foreach {$FirstDay.AddDays($_) | where {$_.DayOfWeek -like "$Weekday*"}})

        # Translate ordinal selection into Index Number
        $Index = switch ($ordinal)
            {
            "1st" {0}
            "2nd" {1}
            "3rd" {2}
            "4th" {3}
            "5th" {4}
            "Last" {-1}
            }

        # Get Target Occurrence
        $Target = ($Occurrences[$index])
        
        if ($Target)
            {
            if ($InjectTime){$Target = $Target.Add($InjectTime)}
            $Target
            }
        else {write-host "There is no $Ordinal $Weekday of $Month $Year" -ForegroundColor Red}
        }
    }