I need to create a PowerShell script, able to collect some data at each minute. However, this script cannot run during the "technical windows", that are scheduled to occur at the same week days and time. (Ex. every tuesday from 19h00 to 23h00) This schedule information is declared in a file (Ex:.\Schedule.txt):
# SCHEDULE
#
# Periods when the script will not be executed: "Prohibited Period"
# Inform: <Begin_Week_Day>:<Begin_Hour>:<Begin_Minute>:<End_Week_Day>:<End_Hour>:<End_Minute>
#
# Example: Set the Prohibited Period of script execution, every Tuesday, from 8:00 pm to 10:00 pm:
# tue:20:00:tue:22:00
#
# ATTENTION: LIMITED SCHEDULE TO THE SAME DAY OF THE WEEK!
#
qua:10:30:qua:15:40
qui:13:30:qui:23:00
In this example, there are two technical windows scheduled.
I achieved this in the first version, as long as the technical window is on the same day:
$ScheduleFile=Get-Content -Path .\Schedule.txt
$NowDay=Get-Date -Format "ddd"
$NowHour=Get-Date -Format "HH"
$NowMinute=Get-Date -Format "mm"
$NowTime=[single]$NowHour+[single]$NowMinute/60
$PeriodoProibido = $false
Write-Host "========================================================================="
Write-Host "SCHEDULE ANALYSIS: Today is ${NowDay}, ${NowHour}:${NowMinute} (${NowTime})"
foreach ($line in $ScheduleFile){
if($line.Substring(0,1) -ne "#") {
Write-Host "-------------------------------------------------------------------------"
$LineArray = $line.Split(':')
<# Schedule: BEGIN of Prohibited Period: $LineArray[0] = Day $LineArray[1] = Hour $LineArray[2] = Minute
END of Prohibited Period: $LineArray[3] = Day $LineArray[4] = Hour $LineArray[5] = Minute
#>
# Convert HOUR to decimal format, include minutes.
$LineArray[1]=[single]$LineArray[1]+[single]$LineArray[2]/60
$LineArray[4]=[single]$LineArray[4]+[single]$LineArray[5]/60
Write-Host "Prohibited Period: From "$LineArray[0] $LineArray[1]" to " $LineArray[3] $LineArray[4]
<# Check if NOW is DAY of Prohibited Period
($NowDay -eq $LineArray[0]) > Check if is DAY do Período Proibido
($NowTime -gt $LineArray[1]) > Check if TIME is AFTER the BEGIN of Prohibited Period
($NowTime -lt $LineArray[4]) > Check if TIME is BEFORE the BEGIN of Prohibited Period
#>
if($NowDay -eq $LineArray[0] -AND $NowTime -gt $LineArray[1] -AND $NowTime -lt $LineArray[4]){
Write-Host "NOW IS A PROHIBITED PERIOD! DO NOT RUN THE SCRIPT."
$PeriodoProibido = $true
}
else {
Write-Host "The script can be executed."
}
}
}
Write-Host "-------------------------------------------------------------------------"
if ($PeriodoProibido) {
Write-Host "CONCLUSION: WE ARE IN THE PROHIBITED PERIOD. DO NOT RUN SCRIPT"
}
else {
Write-Host "CONCLUSION: You can run the script."
}
Write-Host "========================================================================="
The output is similar to:
=========================================================================
SCHEDULE ANALYSIS: Today is qua, 14:42 (14.7)
-------------------------------------------------------------------------
Prohibited Period: From qua 10.5 to qua 15.6666666666667
NOW IS A PROHIBITED PERIOD! DO NOT RUN THE SCRIPT.
-------------------------------------------------------------------------
Prohibited Period: From qui 13.5 to qui 23
The script can be executed.
-------------------------------------------------------------------------
CONCLUSION: WE ARE IN THE PROHIBITED PERIOD. DO NOT RUN SCRIPT
=========================================================================
But, if the technical window starts in a day and ends on the next day, this script will not work. I believe should I have to use date format like in Excel or something similar, but I am not sure.
Does some good ideas to get this script better? Thanks!