locked
Time compare RRS feed

  • Question

  • We would like to execute something by 9 PM daily on the device and if by change the device is offline by that time and boots up by 7 AM next day, then it should not run the program. So I used the following logic, but could not get the required result

    [int]$h = Get-Date -Format HH

    If($h -lt 20 -or $h -gt 7) then {do something } else {exit out}

    Any help please ?

    Tuesday, June 13, 2017 3:01 PM

All replies

  • Hi,

    Your condition is always true.

    Condition for time between 20:00 and 7:00:

    $h -gt 20 -or $h -lt 7

    Condition for time between 7:00 and 20:00

    $h -gt 7 -and  $h -lt 20

    • Proposed as answer by Hello_2018 Wednesday, June 14, 2017 6:04 AM
    Tuesday, June 13, 2017 3:16 PM
  • We would like to execute something by 9 PM daily on the device and if by change the device is offline by that time and boots up by 7 AM next day, then it should not run the program. So I used the following logic, but could not get the required result

    [int]$h = Get-Date -Format HH

    If($h -lt 20 -or $h -gt 7) then {do something } else {exit out}

    Any help please ?

    something like this?

    $date = [datetime]::Now.Hour
    if(($date -eq '9') -and ((Get-Service -Name BITS).Status -like 'stopped'))
    {
        write-host "will boot up by 7 AM"
        sleep 50400 #seconds
        Start-Service -Name BITS
    }else{
        Write-Host "not running duting specified time"
    }

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 14, 2017 6:04 AM
  • By the way, this is not a wrong but an unbeautiful line
    [int]$h = Get-Date -Format HH
    Use
    (Get-Date).Hour
    instead. There's no need to convert DateTime to String with format and then convert it to integer. DateTime has integer properties for all parts of the timestamp, already.
    • Proposed as answer by Hello_2018 Thursday, June 22, 2017 6:46 AM
    Wednesday, June 14, 2017 6:23 AM
  • Hi vinod7,

    any updates?

    Did this issue was resolved?

    Best regards,

    Andy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, June 15, 2017 2:32 AM