Welcome › Forums › General PowerShell Q&A › Get-specificDate
- This topic has 7 replies, 4 voices, and was last updated 10 months ago by
Participant.
-
AuthorPosts
-
-
March 26, 2020 at 3:56 pm #212676
I have a script that a former co worker wrote. It gets a weekday in the future for example 1st sunday in the month. Problem is i need the first sunday in the next month and the code will only return the 1st sunday of the current month. code below. Anyone have any idea’s on how to modify this code to fix? I’ve tried several changes without success.
function Get-SpecificDate
{
[CmdletBinding()]
[OutputType([System.DateTime])]
Param
([Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[ValidateSet(“First”, “Second”, “Third”,”Fourth”,”Fifth”)]
[System.String]
$Instance,[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[System.DayOfWeek]
$Day,[Parameter(ValueFromPipelineByPropertyName=$true,
Position=2)]
[ValidateRange(1,12)]
[int]
$Month = (Get-Date).Month,[Parameter(ValueFromPipelineByPropertyName=$true,
Position=2)]
[ValidateNotNullOrEmpty()]
[int]
$Year = (Get-Date).Year)
[System.DateTime]$TempDate = “{0}/{1}/{2}” -f $Year,$Month,1
While($TempDate.DayOfWeek -ne $Day){
$TempDate = $TempDate.AddDays(1)
}$increment = switch ($Instance)
{
‘First’ {0}
‘Second’ {7}
‘Third’ {14}
‘Fourth’ {21}
‘Fifth’ {28}}
$finalDate = $TempDate.AddDays($increment)
if($finalDate.Month -gt $Month){
Write-Warning -Message (“There is no {0} {1} in {2} ({3})” -f $Instance,$Day,[System.Globalization.DateTimeFormatInfo]::CurrentInfo.GetMonthName($Month),$Year)
}Else{
$finalDate
}
}$day = Get-SpecificDate -Instance First -Day Sunday
$day -
March 26, 2020 at 4:07 pm #212685
Please go back and fix your post by fomratting the code as code using the code tags “PRE”. Thanks. Read Me Before Posting! You’ll be Glad You Did!
-
March 26, 2020 at 4:13 pm #212688
Have you actually tried to understand the function? Just give it the right month as a parameter and it will give you what you want.
PowerShell1Get-SpecificDate -Day Sunday -Month 4 -Instance First -
March 26, 2020 at 4:43 pm #212691
Try this Get-DayOfMonth function of the AZSBTools PS module
PowerShell1Install-Module AZSBTools -Force -AllowClobber -Scope CurrentUser -SkipPublisherCheckFirst Sunday of the current month:
PowerShell12Get-DayOfMonth -DayofWeek Sunday -FirstSunday, March 1, 2020 12:39:43 PMFirst Sunday of the next month:
PowerShell12Get-DayOfMonth -DayofWeek Sunday -Month (Get-Date).AddMonths(1).Month -FirstSunday, April 5, 2020 12:39:09 PMLast Saturday in October 1911
PowerShell12Get-DayOfMonth -DayofWeek Saturday -Month 10 -Year 1911Saturday, October 28, 1911 12:40:42 PMLast Tuesday in July 2165
PowerShell12Get-DayOfMonth -DayofWeek Tuesday -Month 7 -Year 2165Tuesday, July 30, 2165 12:41:50 PMBuilt in help:
PowerShell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113help Get-DayOfMonth -FullNAMEGet-DayOfMonthSYNOPSISFunction to get a given day of the week such as Sunday of a given Month/Year like March/2020SYNTAXGet-DayOfMonth [[-DayofWeek] <String>] [-First] [[-Month] <Int32>] [[-Year] <Int32>] [<CommonParameters>]DESCRIPTIONFunction to get a given day of the week such as Sunday of a given Month/Year like March/2020PARAMETERS-DayofWeek <String>Optional parameter that defaults to 'Sunday'Valid options are 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'Required? falsePosition? 1Default value SundayAccept pipeline input? falseAccept wildcard characters? false-First [<SwitchParameter>]Optional switch parameter. By default it retuns the first day of the monthWhen set to $true, it returns the last day of monthRequired? falsePosition? namedDefault value FalseAccept pipeline input? falseAccept wildcard characters? false-Month <Int32>Optional parameter from 1 to 12Required? falsePosition? 2Default value (Get-Date).MonthAccept pipeline input? falseAccept wildcard characters? false-Year <Int32>Optional parameter from 1 to 10,000Required? falsePosition? 3Default value (Get-Date).YearAccept pipeline input? falseAccept wildcard characters? false<CommonParameters>This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable. For more information, seeabout_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).INPUTSOUTPUTSThis cmdlet returns a DateTime objectNOTESFunction by Sam Boutrosv0.1 - 26 March 2020-------------------------- EXAMPLE 1 --------------------------PS C:\>Get-DayOfMonthThis will return the last Sunday of the current Month/Year as in:Sunday, March 29, 2020 12:26:49 PM-------------------------- EXAMPLE 2 --------------------------PS C:\>Get-DayOfMonth -DayofWeek MondayThis will return the last Monday of the current Month/Year as in:Monday, March 30, 2020 12:27:34 PM-------------------------- EXAMPLE 3 --------------------------PS C:\>Get-DayOfMonth -DayofWeek Saturday -FirstThis will return the first Saturday of the current Month/Year as in:Saturday, March 7, 2020 12:28:25 PM-------------------------- EXAMPLE 4 --------------------------PS C:\>Get-DayOfMonth -DayofWeek Friday -Month 3 -Year 1945This will return the last Friday of March 1945 as in:Friday, March 30, 1945 12:29:54 PMRELATED LINKShttps://superwidgets.wordpress.com/category/powershell/ -
March 26, 2020 at 5:06 pm #212694
The function has a Month param to indicate the month…
PowerShell1234#Static month$day = Get-SpecificDate -Instance First -Day Sunday -Month 4#Or Dynamic month$day = Get-SpecificDate -Instance First -Day Sunday -Month ((Get-Date).AddMonths(1)).Month -
March 26, 2020 at 9:23 pm #212772
Get-DayofMonth works for me. I was able to use that for some automation. Thanks so much for that!
-
March 26, 2020 at 9:53 pm #212778
Did you actually read my answer and the answer from Rob?
-
March 27, 2020 at 4:29 pm #212931
Olaf
Thanks for pointing out the post. I need a dynamic month. Robs answer also works, i didn’t see that. I’m using this in Orchestrator to send out emails with a specific date. Appreciate the input.
-
-
AuthorPosts
- The topic ‘Get-specificDate’ is closed to new replies.