【发布时间】:2014-04-01 14:26:19
【问题描述】:
我正在改进我的监视脚本,因此我可以选择一个服务/维护窗口。在两个时间间隔之间忽略所有错误。
这是我得到的:
Add-Type -TypeDefinition @"
public struct ServiceWindow
{
public int SWStart;
public int SWEnd;
}
"@
[array]$SWArray = New-Object ServiceWindow
$time = Get-Date -Format HHMM
$time
$ActiveBatchVar = "1000-1005;1306-1345;2300-2305"
$ActiveBatchVar = $ActiveBatchVar.Split(";")
For ($i = 0; $i -lt $ActiveBatchVar.Length; $i++)
{
$tempSW = New-Object ServiceWindow
$tempSW.SWStart = $ActiveBatchVar[$i].Split("-")[0]
$tempSW.SWEnd = $ActiveBatchVar[$i].Split("-")[1]
If ($i -eq 0) { $SWArray = $tempSW } else { $SWArray += $tempSW }
}
Write-Host Complete array...
$SWArray
ForEach-Object ($SWArray) {
Get-Date -Format HHMM
If ($time -ge $_.SWStart -and $time -lt $_.SWEnd) {Write-Host Wohoo we have hit a service window service window...}
}
我在上一个 ForEach-Object 循环中遇到错误。并且无法弄清楚出了什么问题。
关键是我想检查当前时间是否在两个给定时间之间,例如“1000-1005”。
任何人都知道缺少什么,或者可能是简化整个事情的方法;)
【问题讨论】:
-
您是否打算将 DateTime 格式设置为 HourHourMonthMonth?或者你的意思是说 HHmm?
-
不,我不是故意的 ;) 感谢您的建议。
标签: arrays powershell struct foreach