none
run every third month RRS feed

  • Question

  • I want to run a powershell script, on the third Thursday of third month. I will be running the script using a gmsa so have to set it up through powershell, and as the monthly option isn't available in New-ScheduledTaskTrigger, I am looking to run the script daily, and check the date within the script to decide whether to run or not.

    so far I have .

    $RunDay = (1..28 | foreach {
     ([datetime](Get-Date).ToString('MM/01/yyyy')).AddDays($_) |
     Where {$_.dayofweek -eq 'Thursday'} } )[2]
     
    $today = [datetime](Get-Date ).ToString('MM/dd/yyyy')


    if ($today -in $RunDay)
        {action}

    Else {exit}

    How can I add months to that, ie run every January,April,July,October ?





    • Edited by MonkeySpacePilot Thursday, February 14, 2019 2:26 PM
    • Moved by Bill_Stewart Friday, March 15, 2019 7:54 PM This is not "scripts on demand"
    Thursday, February 14, 2019 2:25 PM

All replies

  • I managed to work it out myself, I ended up with

    $RunDay = (1..28 | foreach {
        ([datetime](Get-Date).ToString('MM/01/yyyy')).AddDays($_) |
        Where {$_.dayofweek -eq 'Thursday'} } )[2]

    $RunMonth = (0..11 | foreach {
        ([datetime](Get-Date).ToString('1/dd/yyyy')).AddMonths($_) |
        Where {$_.Month}})[0,3,6,9]

    $today = [datetime](Get-Date ).ToString('MM/dd/yyyy')


    if ($today -in $RunDay -And $today -in $RunMonth)
        {"action"}
    ELSE
        {"no-action"}




    Thursday, February 14, 2019 3:55 PM
  • Please format your code as code using the code posting tool provided on the edit bar of the post editor. Thanks.

     If(((Get-Date).Month) % 3  -eq 1) {
        'cool'
     }
     else{
        'not cool'
     }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, February 14, 2019 3:59 PM
    Thursday, February 14, 2019 3:58 PM