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.
==============================