Get-specificDate

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

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!

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.

Get-SpecificDate -Day Sunday -Month 4 -Instance First

Try this Get-DayOfMonth function of the AZSBTools PS module

Install-Module AZSBTools -Force -AllowClobber -Scope CurrentUser -SkipPublisherCheck

First Sunday of the current month:

Get-DayOfMonth -DayofWeek Sunday -First
Sunday, March 1, 2020 12:39:43 PM

First Sunday of the next month:

Get-DayOfMonth -DayofWeek Sunday -Month (Get-Date).AddMonths(1).Month -First
Sunday, April 5, 2020 12:39:09 PM

Last Saturday in October 1911

Get-DayOfMonth -DayofWeek Saturday -Month 10 -Year 1911
Saturday, October 28, 1911 12:40:42 PM

Last Tuesday in July 2165

Get-DayOfMonth -DayofWeek Tuesday -Month 7 -Year 2165
Tuesday, July 30, 2165 12:41:50 PM

Built in help:

help Get-DayOfMonth -Full

NAME
    Get-DayOfMonth
    
SYNOPSIS
    Function to get a given day of the week such as Sunday of a given Month/Year like March/2020
    
    
SYNTAX
    Get-DayOfMonth [[-DayofWeek] <String>] [-First] [[-Month] <Int32>] [[-Year] <Int32>] [<CommonParameters>]
    
    
DESCRIPTION
    Function to get a given day of the week such as Sunday of a given Month/Year like March/2020
    

PARAMETERS
    -DayofWeek <String>
        Optional parameter that defaults to 'Sunday'
        Valid options are 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'
        
        Required?                    false
        Position?                    1
        Default value                Sunday
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -First [<SwitchParameter>]
        Optional switch parameter. By default it retuns the first day of the month
        When set to $true, it returns the last day of month
        
        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -Month <Int32>
        Optional parameter from 1 to 12
        
        Required?                    false
        Position?                    2
        Default value                (Get-Date).Month
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    -Year <Int32>
        Optional parameter from 1 to 10,000
        
        Required?                    false
        Position?                    3
        Default value                (Get-Date).Year
        Accept pipeline input?       false
        Accept wildcard characters?  false
        
    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer, PipelineVariable, and OutVariable. For more information, see 
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216). 
    
INPUTS
    
OUTPUTS
    This cmdlet returns a DateTime object
    
    
NOTES
    
    
        Function by Sam Boutros
        v0.1 - 26 March 2020
    
    -------------------------- EXAMPLE 1 --------------------------
    
    PS C:\>Get-DayOfMonth
    
    This 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 Monday
    
    This 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 -First
    
    This 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 1945
    
    This will return the last Friday of March 1945 as in:
    Friday, March 30, 1945 12:29:54 PM
    
RELATED LINKS
    https://superwidgets.wordpress.com/category/powershell/

The function has a Month param to indicate the month…

#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

Get-DayofMonth works for me. I was able to use that for some automation. Thanks so much for that!

Did you actually read my answer and the answer from Rob?

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.