Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop while week day + Business hours

Tags:

powershell

I am writing a script where it will only run during M-F 8AM-5PM. The problem is after it falls off either 8-5 or M-F while() loop it just... doesn't know how to get back into the loop. This makes me think that I may be approaching my script at the wrong angle. Maybe there's a better way of doing this.

The "do something" part is it's comparing file size between the same file at 5 minutes apart. This is for checking if the file is growing/shrinking or not.

# Declare current date/time
$Weekday = [int](Get-Date).DayOfWeek
$hour = [int](Get-Date -Format HH)

while (1 -eq 1) {   # always true
    $Weekday = [int](Get-Date).DayOfWeek
    $hour = [int](Get-Date -Format HH)
    while ($Weekday -ge 1 -and $Weekday -le 5) {  # weekday
        $Weekday = [int](get-date).DayOfWeek    # loop check for current day
        while ($hour -ge 8 -and $hour -le 16) { # 8AM-5PM
            $hour = [int](get-date -format HH)  # loop check for current hour
            # Do Something
        }
        else {
            # Sleep until next business hour
            $date = Get-Date
            $date = $date.AddDays(1)
            $mmddyyy = $date.ToString("MM/dd/yyy")
            $nextDy = New-TimeSpan -End "$mmddyyy 08:00"
            Write-Host "Start sleep timer until next 8AM"
            Start-Sleep -Seconds $nextDy.TotalSeconds
        }
    }
    else {
        # Sleep until next business day
        $date = Get-Date
        while ($Date.DayOfWeek -ne "Monday") {$date = $date.AddDays(1)}
        $mmddyyy = $date.ToString("MM/dd/yyy")
        $nextBu = New-TimeSpan -End "$mmddyyy 08:00"
        Write-Host "Start sleep timer until next Monday 8AM"
        Start-Sleep -Seconds $nextBu.TotalSeconds
    }
}
like image 344
stess Avatar asked Feb 18 '26 10:02

stess


2 Answers

  1. Try using scheduled task or service.

  2. If you want to use a PowerShell script try using this:

    while ($true)
    {
        #WeekDay
        $Weekday = [int](Get-Date).DayOfWeek #loop check for current day
        $hour = [int](Get-Date -Format HH)
        if ($Weekday -le 5 -and $Weekday -ge 1)
        {
            while ($hour -ge 8 -and $hour -le 16)
            {
                #8AM-5PM
                $hour = [int](Get-Date -Format HH) #loop check for current hour
                #Do Something
            }
        }
        $date = Get-Date
        $date = $date.AddDays(1)
        $mmddyyy = $date.ToString("MM/dd/yyy")
        $nextDy = New-TimeSpan -End "$mmddyyy 08:00"
        Write-Host "Start sleep timer until next 8AM"
        Start-Sleep -Seconds $nextDy.TotalSeconds
    }
    
like image 57
M.Pinto Avatar answered Feb 19 '26 22:02

M.Pinto


I think you got it totally wrong.

Better you use a simple library function like TestFileSizeUtil to monitor file size or use WMI like shown here:

$query = "Select * from __InstanceModificationEvent WITHIN 5 WHERE TargetInstance ISA 'CIM_DataFile' AND TargetInstance.Name='C:\\Logs\\test.log'"

Register-WmiEvent -Query $query -Action {
    Write-Host "Current file size is: " $Event.SourceEventArgs.NewEvent.TargetInstance.FileSize
    $prevSize = $Event.SourceEventArgs.NewEvent.PreviousInstance.FileSize
    $curSize = $Event.SourceEventArgs.NewEvent.TargetInstance.FileSize
    if ($curSize -gt $prevSize) {
        $bytes = $curSize - $prevSize
        Write-Host "File grew by: $bytes bytes"
    } else {
        $bytes = $prevSize - $curSize
        Write-Host "File reduced by: $bytes bytes"
    }
}

Then, it should not matter if your script runs all day long.

like image 34
wp78de Avatar answered Feb 19 '26 22:02

wp78de