Reputation: 87
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
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 ...
#region/#endregion
block and replace it with a call to your data source. .Hour
of the current object is in the $ExcludedHours
range 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