我希望根据是否是营业时间来运行条件声明.我们的营业时间是08:00至17:00.我有下面的脚本,但它不起作用.
我想(Get-Date).tostring('%H')与小时数进行比较.
我也尝试过((Get-Date).hour -ge 17)它仍然失败了.
有什么想法吗?
while ($loop -eq 1) {
Write-Host "Running"
# Get last write timestamp
$lastwrite = [datetime](Get-ItemProperty -Path $source -Name LastWriteTime).lastwritetime
if ( ((Get-Date).tostring('%H') -le "8" ) -and ( (Get-Date).tostring('%H') -ge "17" ) ) {
# Do nothing, it is outside of the time window
Write-Host "Nothing to do, outside of business hours"
} elseif (($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)) -and ((Get-Date).tostring('%H') -ge "8" -and (Get-Date).tostring('%H') -le "17")) {
# If it's older than $ageMinutes variable above, send an email
notify
$oldTimestampFound = 1
# Sleep for 4 minutes to not flood inboxes (5 minute sleep total with the while loop)
Write-Host "Alert sent. Sleeping for 4 minutes..."
Start-Sleep -s 300
} elseif (($lastwrite -ge (Get-Date).addMinutes(-$ageMinutes)) -and ($oldTimestampFound -eq 1)) {
$oldTimestampFound = 0
Write-Host "All clear"
notifyAllClear
}
Write-Host "Sleeping for 60 seconds..."
Start-Sleep -s 60
}
Run Code Online (Sandbox Code Playgroud)
我把那些Write-Hosts放在那里尝试调试,但我的输出是
Running
Sleeping for 60 seconds...
Run Code Online (Sandbox Code Playgroud)
您正在格式化Get-Date错误,请单独使用Get-Date -Format HH选项我将其设置为整数一次,以便于比较,如下所示:
[int]$hour = get-date -format HH
If($hour -lt 8 -or $hour -gt 17){ <do nothing> }
Else{
If($lastwrite -le (Get-Date).addMinutes(-$ageMinutes)){ <send email, set oldTimeStampFound flag, and sleep> }
Else{
<Clear oldTimeStampFound flag>
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9777 次 |
| 最近记录: |