none
Powershell get-date 1st and 3rd Wednesday RRS feed

  • Question

  • I'm trying to run a script every the 1st and 3rd Wednesday of each month.

    Any ideals?

    Thanks,

    Kevin

    Friday, December 5, 2014 3:43 PM

Answers

  • Try this:

    $date = [datetime](get-date).ToShortDateString()
    
    if ($date.DayOfWeek -eq 'Wednesday' -and $date.ToOADate() % 2)
    
    {"Match - Go"}
    ELSE
        {"No Match"}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by bakerkr94 Friday, December 5, 2014 6:58 PM
    Friday, December 5, 2014 6:43 PM
    Moderator

All replies

  • I poked at it and got this:

    $Wednesdays = (1..21 | foreach {
     ([datetime](Get-Date).ToString('MM/01/yyyy')).AddDays($_) |
     Where {$_.dayofweek -eq 'Wednesday'} } )[0,2]
     
    
     $Wednesdays
    
    Wednesday, December 03, 2014 12:00:00 AM
    Wednesday, December 17, 2014 12:00:00 AM


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Friday, December 5, 2014 3:59 PM
    Moderator
  • Using your code, I added the following:

    $today = [datetime](Get-Date ).ToString('MM/dd/yyyy')
    if ($today -eq $Wednesdays) 
        {"Match - Go"}
    ELSE
        {"No Match"}
    
    $Wednesdays
    $today
    But this results in No Match, $Wednesday contains two dates and I'm not sure how to get around this.



    • Edited by bakerkr94 Friday, December 5, 2014 5:17 PM updated
    Friday, December 5, 2014 4:54 PM
  • changed to:

    if($today -in $Wednesdays)

    that worked

    Friday, December 5, 2014 5:20 PM
  • You could also use 

    if ($Wednedays -eq [datetime]::today)


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, December 5, 2014 5:45 PM
    Moderator
  • So I now realize there is a flaw.. for December there are 5 Wednesdays, meaning the code would skip the 24 and the 31 before running again.  Is it possible to run the code every other Wednesday??
    Friday, December 5, 2014 6:04 PM
  • That means it's not working on a monthly basis. Assuming you don't want it to store a 'last run' somewhere you'd probably have to adjust it to loop through the entire year.

    $AllWednesdays = (0..365 | foreach {
     ([datetime](Get-Date).ToString('01/01/yyyy')).AddDays($_) |
     Where {$_.dayofweek -eq 'Wednesday'} } )
    
    $evenWednesdays = @()
    for ($counter = 0; $counter -lt $allWednesdays.count; $counter++)
    {
     if ($counter % 2 -eq 0)
    {
    $evenWednesdays += $AllWednesdays[$counter]
    }
    }

    I'm sure there's a much nicer way to do it though.

    Friday, December 5, 2014 6:29 PM
  • do it by week number.

    ¯\_(ツ)_/¯

    Friday, December 5, 2014 6:42 PM
  • Try this:

    $date = [datetime](get-date).ToShortDateString()
    
    if ($date.DayOfWeek -eq 'Wednesday' -and $date.ToOADate() % 2)
    
    {"Match - Go"}
    ELSE
        {"No Match"}


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by bakerkr94 Friday, December 5, 2014 6:58 PM
    Friday, December 5, 2014 6:43 PM
    Moderator