-
Notifications
You must be signed in to change notification settings - Fork 4
/
Get-SpecificDate.ps1
111 lines (104 loc) · 3.12 KB
/
Get-SpecificDate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function Get-SpecificDate
{
<#
.SYNOPSIS
Finds a specific instance of the day of the week.
.DESCRIPTION
This function can be used to find an instance of a day of the week.
For example, you can ask for the first Monday of the month.
.PARAMETER Instance
Which week day instance to return for selected month.
Valid options are: First, Second, Third, Fourth, Fifth, Last
.PARAMETER Day
Day of week to return. Options should be DayOfWeek type.
.PARAMETER Month
Which month to select. Value should be between 1 and 12.
Defaults to current month.
.PARAMETER Year
Whcih year to select. Default value is current year.
.EXAMPLE
PS C:\> Get-SpecifcDay -Instance Second -Day Tuesday
Returns a DateTime object representing the second Tuesday of the
current month
.EXAMPLE
PS C:\> 1..12 | Get-SpecificDate Second Tuesday
Lists all patch Tuesdays for the current year.
.OUTPUTS
DateTime
.Link
overpowershell.com
#>
[CmdletBinding()]
[OutputType([DateTime])]
Param
(
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[ValidateSet("First", "Second", "Third", "Fourth", "Fifth", "Last")]
[string]
$Instance,
[Parameter(
Mandatory = $true,
ValueFromPipelineByPropertyName = $true,
Position = 1)]
[DayOfWeek]
$Day,
[Parameter(
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 2)]
[ValidateRange(1, 12)]
[int]
$Month = (Get-Date).Month,
[Parameter(
ValueFromPipelineByPropertyName = $true,
Position = 3)]
[ValidateNotNullOrEmpty()]
[int]
$Year = (Get-Date).Year
)
Process
{
[datetime]$tempDate = "{0}/{1}/{2}" -f $Year, $Month, 1
while ($tempDate.DayOfWeek -ne $Day)
{
$tempDate = $tempDate.AddDays(1)
}
if ($Instance -eq 'Last')
{
$longMonth = $tempDate.AddDays(28).Month -eq $Month
if ($longMonth)
{
$finalDate = $tempDate.AddDays(28)
}
else
{
$finalDate = $tempDate.AddDays(21)
}
}
else
{
$increment = switch ($Instance)
{
'First' {0}
'Second' {7}
'Third' {14}
'Fourth' {21}
'Fifth' {28}
}
$finalDate = $tempDate.AddDays($increment)
}
if ($finalDate.Month -gt $Month)
{
$monthFriendlyName = [Globalization.DateTimeFormatInfo]::CurrentInfo.GetMonthName($Month)
$message = ("There is no {0} {1} in {2} ({3})" -f $Instance, $Day, $monthFriendlyName, $Year)
throw [IndexOutOfRangeException]::New($message)
}
Else
{
$finalDate
}
}
}