Ayush Lal
Ayush Lal

Reputation: 87

IF between two times powershell

I am trying to figure out if an issue happened during a certain period of time however for what ever reason nothing seems to be working.

Here is my powershell.ps1 file

$AUS_start = Get-Date -Hour 22 -Minute 00 -Second 00
$AUS_end = Get-Date -Hour 06 -Minute 00 -Second 00
$AS = $AUS_start.ToString("HH:mm:ss")
$AE = $AUS_end.ToString("HH:mm:ss")

foreach ($SHtime in $Rservices.start_time) {
    $x = $SHtime.ToString("HH:mm:ss")
    $x
    if ($x -gt $AS -and $x -lt $AE) {
        Write-Host "true"
    }
    else {
        Write-Host "false"
    }
}

Here is the response I get

22:18:01
false
19:11:00
false
05:15:00
false
05:15:00
false
02:36:43
false

As you can see, there are certain times for example "22:18:01" that definitely meet the criteria of greater than UTC 22:00:00 and less than 06:00:00 but it still returns a false value.

Any ideas?

Upvotes: 0

Views: 1630

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7479

this does what i think you want. [grin]

instead of testing for in 2200-0600 it tests for NOT in 0600-2200. that test is simpler since it gives one a continuous range that is all in one day. if you need to allow 2200-2259, you can simply shift the $EndHour to 21.

please note that this explicitly ignores the date and only uses the hour-of-day. if you need to look at the date, that will require some extra code. if i understand your intent, tho, that seems unlikely.

what the code does ...

  • creates a set of datetime objects to work with
    when ready to work with real data, simply remove the entire #region/#endregion block and replace it with a call to your data source.
  • sets the start/end hours
  • builds the excluded hours range
  • iterates thru the collection of test datetime objects
  • checks to see if the .Hour of the current object is in the $ExcludedHours range
  • if yes, writes a warning to the screen to not disturb the "normal working hours" folks
  • if NO, writes a msg that it is safe to do IT work while the office folks are all far away

here's the code ...

#region >>> create some date time items to test with
#    when ready to use real data, replace the entire "#region/#endregion" block with the real data source
$RServices = @(
    [PSCustomObject]@{
        Name = 'LastYearElevenP'
        Start_Time = (Get-Date).Date.AddYears(-1).AddHours(23)
        },
    [PSCustomObject]@{
        Name = 'LastMonthElevenP'
        Start_Time = (Get-Date).Date.AddMonths(-1).AddHours(23)
        },
    [PSCustomObject]@{
        Name = 'YesterdayElevenP'
        Start_Time = (Get-Date).Date.AddDays(-1).AddHours(23)
        },
    [PSCustomObject]@{
        Name = 'ZeroA'
        Start_Time = (Get-Date).Date
        },
    [PSCustomObject]@{
        Name = 'OneA'
        Start_Time = (Get-Date).Date.AddHours(1)
        },
    [PSCustomObject]@{
        Name = 'ThreeA'
        Start_Time = (Get-Date).Date.AddHours(3)
        },
    [PSCustomObject]@{
        Name = 'SixA'
        Start_Time = (Get-Date).Date.AddHours(6)
        },
    [PSCustomObject]@{
        Name = 'SevenA'
        Start_Time = (Get-Date).Date.AddHours(7)
        },
    [PSCustomObject]@{
        Name = 'NineA'
        Start_Time = (Get-Date).Date.AddHours(9)
        },
    [PSCustomObject]@{
        Name = 'ElevenP'
        Start_Time = (Get-Date).Date.AddHours(23)
        }
    )
#endregion >>> create some date time items to test with

$StartHour = 6
$EndHour = 22
$ExcludedHours = $StartHour..$EndHour

foreach ($RS_Item in $RServices)
    {
    if ($RS_Item.Start_Time.Hour -in $ExcludedHours)
        {
        Write-Warning ('    {0} is in the Excluded Hours ...' -f $RS_Item.Start_Time.ToString('yyyy-MM-dd HH:mm:ss'))
        Write-Warning '        Do nothing disruptive at this time.'
        }
        else
        {
        '{0} is NOT in the excluded hour range.' -f $RS_Item.Start_Time.ToString('yyyy-MM-dd HH:mm:ss')
        '    Now is the time to do things that the office folks might complain about.'
        }
    '=' * 30
    }

the screen output ...

2019-04-29 23:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
2020-03-29 23:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
2020-04-28 23:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
2020-04-29 00:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
2020-04-29 01:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
2020-04-29 03:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================
WARNING:     2020-04-29 06:00:00 is in the Excluded Hours ...
WARNING:         Do nothing disruptive at this time.
==============================
WARNING:     2020-04-29 07:00:00 is in the Excluded Hours ...
WARNING:         Do nothing disruptive at this time.
==============================
WARNING:     2020-04-29 09:00:00 is in the Excluded Hours ...
WARNING:         Do nothing disruptive at this time.
==============================
2020-04-29 23:00:00 is NOT in the excluded hour range.
    Now is the time to do things that the office folks might complain about.
==============================

Upvotes: 1

Related Questions