Exit script IF current date is December 24th or 25th regardless of future years

This topic contains 5 replies, has 4 voices, and was last updated by  sg68 3 months, 3 weeks ago.

  • Author
    Posts
  • #90014

    sg68
    Participant

    Hello!

    Exit .ps1 script IF current dates Decemeber 24th or December 25th regardless of future years. (Please, Leap year should be addressed)

    PowerShell version 2.0

    Your help will be really appreciated!

    Best Regards

  • #90019

    Shihan Pietersz
    Participant

    HI Sg68,
    not 100% sure what your looking for. But there are many ways to do achieve this. if your not concerned about the year try the code below

    $Date1 = "24/12"
    $Date2 ="25/12"
    
    $CurrentDate = (Get-Date -UFormat "%d/%m").ToString()
    
    if(($Date1 -eq $CurrentDate) -or ($Date2 -eq "$CurrentDate")){
        Write-Host "Current Date is $(Get-Date -UFormat "%d/%m/%Y")"
        Exit
    }
    else{
        Write-Host "Current date is not $($Date1) or $($Date2)"
    
    }
    

    Regards
    Shihan

  • #90020

    postanote
    Participant

    Try this...

    If((Get-Date -UFormat '%d/%m') -match '24/12|25/12')
    {Write-Warning -Message 'Current date is 24 or 25 December. Exiting process!';Exit}
    Else{'Current date is not 24 or 25 December. Process continuing'}

    • #90022

      sg68
      Participant

      Wow, Simple but smart!!!
      Thank you so much for your help! Will definitely try it.
      Regards

  • #90029

    Ron
    Participant

    Get-Date returns an object with lots of information in it.

    > Get-Date | fl *
    
    DisplayHint : DateTime
    DateTime    : Tuesday, December 26, 2017 11:18:38 AM
    Date        : 12/26/2017 12:00:00 AM
    Day         : 26
    DayOfWeek   : Tuesday
    DayOfYear   : 360
    Hour        : 11
    Kind        : Local
    Millisecond : 109
    Minute      : 18
    Month       : 12
    Second      : 38
    Ticks       : 636498839181097017
    TimeOfDay   : 11:18:38.1097017
    Year        : 2017
    
    
    > $d=Get-Date
    > $d.Month
    > $d.Day
    
    12
    26
    • #90040

      sg68
      Participant

      Thank you for your reply!

You must be logged in to reply to this topic.