I am working on a Powershell script to adjust configs of security cameras at dusk and dawn (you'd think the vendor would have a scheduling function...). What it's supposed to do is use sunrise-sunset.org to pull the Civil Twilight values, start checking to see when it's dawn (incrementing from large to small timeframes), and then kick off an AHK script to make the changes in the web config. The script then pauses for six hours before starting to check for dusk and then kicks off another AHK script.
However, I know I have errors as it doesn't seem to run. For example, while the finalized script will be set with Task Scheduled to start at 4am, I manually kick it off before I go to bed, but when I get up it's still sitting at "At least 120 minutes to dawn". But if I look at the values of Dawn
and Now
, it "should" have worked. Additionally, if I remove the API part and literally just set a "then" (let's say in 30 mins) and "now" and use a couple of do-until loops, it works.
So,
A) any ideas on what is wrong? B) I am near certain this code can be cleaned up/shortened.
# Clear old variables
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
# Get Civil Twilight values and "now"
$Daylight = (Invoke-RestMethod "https://api.sunrise-sunset.org/json?lat=35.608081&lng=-78.647666&formatted=0").results
$Dawn = [datetime] $Daylight.civil_twilight_begin
$Dusk = [datetime] $Daylight.civil_twilight_end
$Now = Get-Date
# Start iterations until dawn
do {
"At least 120 minutes til dawn."
Start-Sleep -S 7200
} until([datetime]::Now -ge $Dawn.AddMinutes(-120))
do {
"At least 60 minutes til dawn."
Start-Sleep -S 3600
} until([datetime]::Now -ge $Dawn.AddMinutes(-60))
do {
"At least 30 minutes til dawn."
Start-Sleep -S 1800
} until([datetime]::Now -ge $Dawn.AddMinutes(-30))
do {
"At least 15 minutes til dawn."
Start-Sleep -S 900
} until([datetime]::Now -ge $Dawn.AddMinutes(-15))
do {
"At least 10 minutes til dawn."
Start-Sleep -S 600
} until([datetime]::Now -ge $Dawn.AddMinutes(-10))
do {
"At least 5 minutes til dawn."
Start-Sleep -S 30
} until([datetime]::Now -ge $Dawn.AddMinutes(-5))
do {
"At least 3 minutes til dawn."
Start-Sleep -S 180
} until([datetime]::Now -ge $Dawn.AddMinutes(-3))
do {
"At least a minute til dawn."
Start-Sleep -S 60
} until([datetime]::Now -ge $Dawn.AddSeconds(-60))
do {
"About to execute..."
Start-Sleep -S 30
} until([datetime]::Now -ge $Dawn.AddSeconds(-30))
# Execute day script
C:\AHK\day.exe | Invoke-Expression
# Rest til the afternoon
Start-Sleep -S 21600
# Update "now"
$Now = Get-Date
# Start iterations until dusk
do {
"At least 120 minutes til dusk."
Start-Sleep -S 7200
} until([datetime]::Now -ge $Dusk.AddMinutes(-120))
do {
"At least 60 minutes til dusk."
Start-Sleep -S 3600
} until([datetime]::Now -ge $Dusk.AddMinutes(-60))
do {
"At least 30 minutes til dusk."
Start-Sleep -S 1800
} until([datetime]::Now -ge $Dusk.AddMinutes(-30))
do {
"At least 15 minutes til dusk."
Start-Sleep -S 900
} until([datetime]::Now -ge $Dusk.AddMinutes(-15))
do {
"At least 10 minutes til dusk."
Start-Sleep -S 600
} until([datetime]::Now -ge $Dusk.AddMinutes(-10))
do {
"At least 5 minutes til dusk."
Start-Sleep -S 30
} until([datetime]::Now -ge $Dusk.AddMinutes(-5))
do {
"At least 3 minutes til dusk."
Start-Sleep -S 180
} until([datetime]::Now -ge $Dawn.AddMinutes(-3))
do {
"At least a minute til dusk."
Start-Sleep -S 60
} until([datetime]::Now -ge $Dusk.AddSeconds(-60))
do {
"About to execute..."
Start-Sleep -S 30
} until([datetime]::Now -ge $Dusk.AddSeconds(-30))
# Execute night script
C:\AHK\night.exe | Invoke-Expression
# Rest 5 minutes
Start-Sleep -S 300
# Clear variables and end
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
Exit
EDIT: This is the current script with all credit to @Darin
Darin, refer to my question below....
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear();
function SleepUntil {...}
function ReportRelativeTimeIfSleepUntil {...}
function GetApiTodayTomorrow {...}
function GetNextChange {...}
function DoCountDown {...}
DoCountDown
Start-Sleep S 600
DoCountDown