0

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?

10
  • 2
    It is best to not convert times to strings. Is the intent to compare times, ignoring the date? Also, consider the date for 06:00:00 - the code doesn't include that. Commented Apr 28, 2020 at 10:23
  • You're converting the dates to strings - just leave them as date time objects and the comparison should work. Commented Apr 28, 2020 at 10:26
  • @AndrewMorton correct sir! how would I go about converting them to strings - I have done a .GetType() on $x, $AE & $AS and they both show up as string system.object? Commented Apr 28, 2020 at 10:29
  • @auburg I need to compare them based on times not date Commented Apr 28, 2020 at 10:29
  • 1
    @AyushLal Then create a TimeSpan object learn.microsoft.com/en-us/powershell/module/… Commented Apr 28, 2020 at 10:40

1 Answer 1

1

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.
==============================
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks heaps Lee. Although from my API call using the .hour im getting the following hours: 14, 12, 11, 7. I have set my $start to 22 and $end to 6. but its still telling me that those hours are within 22:00 - 06:00... Any ideas?
DO NOT use 2200-0600. that is not a valid range. it also spans TWO DIFFERENT DAYS. [grin] use 0600-2200 and test for NOT IN that range. my code lets you do the "it is safely outside of the protected hours" on the else branch of the if test. please, use the logical and predictable "smaller thru larger" range as the test range. it makes life [and code] so very very very much simpler.
Omg of course how stupid of me! Thank you so much sir, you are a life saver!!
@AyushLal - you are most welcome! glad to have helped somewhat ... [grin]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.