繁体   English   中英

IF 两次之间 powershell

[英]IF between two times powershell

我试图弄清楚问题是否在一段时间内发生,但是出于某种原因似乎没有任何效果。

这是我的 powershell.ps1 文件

$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"
    }
}

这是我得到的回复

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

如您所见,某些时间(例如“22:18:01”)肯定满足大于 UTC 22:00:00 且小于 06:00:00 的条件,但它仍然返回 false 值。

有任何想法吗?

这就是我认为你想要的。 [咧嘴笑]

而不是测试in 2200-0600它测试NOT in 0600-2200 该测试更简单,因为它提供了一个连续的范围,即在一天之内 如果您需要允许2200-2259 ,您可以简单地将$EndHour21

请注意,这明确忽略了日期,只使用一天中的小时。 如果您需要查看日期,则需要一些额外的代码。 如果我理解你的意图,那似乎不太可能。

代码的作用...

  • 创建一组要使用的日期时间对象
    当准备好使用真实数据时,只需删除整个#region/#endregion块并将其替换为对数据源的调用即可。
  • 设置开始/结束时间
  • 构建排除的时间范围
  • 遍历测试日期时间对象的集合
  • 检查当前.Hour的 .Hour 是否在$ExcludedHours范围内
  • 如果是,在屏幕上写一个警告,不要打扰“正常工作时间”的人
  • 如果否,则写一条消息说在办公室人员都在很远的地方进行 IT 工作是安全的

这是代码...

#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
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM