Get-specificDate

Welcome Forums General PowerShell Q&A Get-specificDate

Viewing 7 reply threads
  • Author
    Posts
    • #212676
      Participant
      Topics: 1
      Replies: 2
      Points: -4
      Rank: Member

      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

    • #212685
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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!

    • #212688
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      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.

    • #212691
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero

      Try this Get-DayOfMonth function of the AZSBTools PS module

      First Sunday of the current month:

      First Sunday of the next month:

      Last Saturday in October 1911

      Last Tuesday in July 2165

      Built in help:

    • #212694
      Participant
      Topics: 17
      Replies: 1958
      Points: 4,020
      Helping Hand
      Rank: Community Hero

      The function has a Month param to indicate the month…

    • #212772
      Participant
      Topics: 1
      Replies: 2
      Points: -4
      Rank: Member

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

    • #212778
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

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

    • #212931
      Participant
      Topics: 1
      Replies: 2
      Points: -4
      Rank: Member

      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.

Viewing 7 reply threads
  • The topic ‘Get-specificDate’ is closed to new replies.