1
\$\begingroup\$

So I am trying to create a function in PowerShell that:

  • Takes start time in form HHmm to denote beginning of time window
  • Takes an end time in format HHmm to denote end of time window
  • Calling the function outside that time window results in that function not returning till we have entered the time window again
  • Optionally I can decide how regularly the polling for time happens

Function Stop-OutsideRunningHours
{
    [cmdletbinding()]
    PARAM
    (
        [Parameter( Mandatory = $True )]
        [string]
        $StartTime = "1800",

        [string]
        $FinishTime = "0800",

        [int]
        $SleepFor = 60
    )

    $StartHour = 0
    $StartMinute = 0
    $EndHour = 0
    $EndMinute = 0

    If( $StartTime -eq $EndTime )
    {
        Throw "StartTime cannot be the same as EndTime"
    }

    If( $StartTime -match "(?<StartHour>\d{2})(?<StartMinute>\d{2})" )
    {
        If( $Matches.StartHour -eq $Null -or $Matches.StartMinute -eq $Null )
        {
            Throw "Unable to process start time $( $StartTime )"
        }

        $StartHour = [int]::Parse( $Matches.StartHour )
        $StartMinute = [int]::Parse( $Matches.StartMinute )
    }
    Else
    {
        Throw "StartTime not in expected format: $( $EndTime )"
    }

    If( $FinishTime -match "(?<EndHour>\d{2})(?<EndMinute>\d{2})" )
    {
        If( $Matches.EndHour -eq $Null -or $Matches.EndMinute -eq $Null )
        {
            Throw "Unable to process start time $( $EndTime )"
        }

        $EndHour = [int]::Parse( $Matches.EndHour )
        $EndMinute = [int]::Parse( $Matches.EndMinute )
    }
    Else
    {
        Throw "EndTime not in expected format: $( $EndTime )"
    }


    $Now = Get-Date

    $ExactStart = $Now.Date.AddHours( $StartHour ).AddMinutes( $StartMinute )

    $ExactFinish = $Now.Date
    If( $EndHour -lt $StartHour -or
        ( $EndHour -eq $StartHour -and $EndMinute -lt $StartMinute ) ) { $ExactFinish = $ExactFinish.AddDays( 1 ) }

    $ExactFinish = $ExactFinish.AddHours( $EndHour ).AddMinutes( $EndMinute )

    If( $ExactStart -lt $Now -and $ExactFinish -lt $Now )
    {
        $ExactStart = $ExactStart.AddDays( 1 )
        $ExactFinish = $ExactFinish.AddDays( 1 )
    }

    $FirstPass = $True

    Do
    {
        $CurrentTime = Get-Date

        If( $ExactStart -lt $CurrentTime -and $ExactFinish -lt $CurrentTime )
        {
            $ExactStart = $ExactStart.AddDays( 1 )
            $ExactFinish = $ExactFinish.AddDays( 1 )
        }

        If( -not $FirstPass )
        {
            sleep -Seconds $SleepFor
        }

        $FirstPass = $False
    }
    While( $CurrentTime -lt $ExactStart -and $CurrentTime -lt $ExactFinish )
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What does it do exactly? \$\endgroup\$ – Dangph Jul 27 '17 at 1:28
  • \$\begingroup\$ I began and almost finished before it became clear your question is just not clear enough to produce a full answer. Need to know if this is taking input of hours and minutes as START AND FINISH TIMES for a WINDOW, or as the later code seems to almost suggest instead - hour and minute OFFSETS from the time. If this is in essence, as I suspect, the purpose of setting a "processing window of operation" then please confirm and I can complete what I have - which is very much an optimized version of your code but really can optimize much further if it is in fact just an operation window and otherwis \$\endgroup\$ – Collin Chaffin Jul 27 '17 at 19:36
  • 2
    \$\begingroup\$ 1. Assign a value to $EndTime. 2. Compute seconds remaining to desired time and Start-Sleep for that instead of looping every minute. \$\endgroup\$ – JosefZ Jul 27 '17 at 19:45
  • \$\begingroup\$ @CollinChaffin Clarifications added :) \$\endgroup\$ – Bruno Jul 27 '17 at 23:30
  • \$\begingroup\$ @JosefZ Will that still work if I hibernate my laptop? \$\endgroup\$ – Bruno Jul 27 '17 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.