gpt4 book ai didi

powershell - 循环 while 工作日 + 营业时间

转载 作者:行者123 更新时间:2023-12-03 10:00:24 36 4
gpt4 key购买 nike

我正在编写一个脚本,它只在周一至周五上午 8 点至下午 5 点期间运行。问题是在它脱离 8-5 或 M-F while() 循环之后,它只是......不知道如何回到循环中。这让我觉得我可能以错误的角度接近我的剧本。也许有更好的方法。

“做某事”部分是在相隔 5 分钟的时间比较同一文件的文件大小。这是为了检查文件是否在增长/缩小。

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

最佳答案

  1. 尝试使用计划任务或服务。

  2. 如果您想使用 PowerShell 脚本,请尝试使用此脚本:

    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
    }

关于powershell - 循环 while 工作日 + 营业时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47982962/

36 4 0
文章推荐: javascript - 将 声明转换为 javascript ActiveXObject