Scheduling Within a PowerShell Script
Jump to navigation
Jump to search
Script to run a function at a specific interval throughout the day
$start = Get-Date
# Scheduler stuff...
$DoSingleRun = 0 # Ignore scheduling and just run once (useful for debugging main function)
$IntervalMins = 15 # Should fit into an hour exactly
$End = "18:50" # Time of day that script should cease (should be just after last required run time)
$CheckThrottle = 250 # Idle throttle / time check interval (msec)
# Scheduled and helper functions ================================================================
function Log ($text) {
$stamp = (Get-Date).ToString("HH:mm:ss.fff")
Write-Host "$stamp | $text"
}
function Do-Stuff {
# This is where the business of the script goes
}
function Log-Perf {
# Keeps track of script resource usage
Log ("Perf CPU(sec): " + [Math]::Round($proc.cpu, 0) + ", Paged Mem (MB): " + [Math]::Round(($proc.pm/1024), 0) + ", WrkSet Mem (MB): " + Math]::Round(($proc.ws/1024), 0))
}
# MAIN SCRIPT ====================================================================================
# Initial prep -----------------------------------------------------------------------------------
if ($DoSingleRun) {
Start-Transcript -Path check-test.log -Append
} else {
Rename-Item -Path check.log -NewName check-1.log -Force
Start-Transcript -Path check.log -Append
}
Log "Started script run at $start"
# Get process for this script
$proc = [System.Diagnostics.Process]::GetCurrentProcess()
# Check scheduler variables
if ((60 % $IntervalMins) -ne 0) {
Log "Interval error - $IntervalMins mins doesn't fit into an hour!"
Exit
}
try {
$EndTime = Get-Date $End
} catch {
Log "Invalid end time: $End hrs"
Log $_.Exception.GetType().FullName
Log $_.Exception.Message
Exit
}
# Set dummy last run time (aligned to nice start time), and next run time
$LastRunTime = Get-Date
$offset = ($IntervalMins + ($LastRunTime.Minute % $IntervalMins))
$LastRunTime = $LastRunTime.AddMinutes(-$offset)
$LastRunTime = $LastRunTime.AddSeconds(-$LastRunTime.Second)
$NextRunTime = $LastRunTime.AddMinutes($IntervalMins)
#Write-Host "Offset is " $offset
Log ("Last runtime is " + $LastRunTime)
Log ("Next runtime is " + $NextRunTime)
Log ("Script cease at " + $EndTime)
# Do any once-off initialisation here,
# eg connect to a MySQL database, open an "I'm alive" TCP port, load credentials
# Main loop -------------------------------------------------------------------------------------
if ($DoSingleRun) {
Log ("Doing single run...")
Do-Stuff
} else {
While (1) {
if ($NextRunTime -lt (Get-Date)) {
Log-Perf
Log ("Starting run at " + (Get-Date))
Do-Stuff
Log ("Completed run at " + (Get-Date))
$LastRunTime = $LastRunTime.AddMinutes($IntervalMins)
$NextRunTime = $LastRunTime.AddMinutes($IntervalMins)
if ($NextRunTime -lt (Get-Date)) {
Log "WARNING: Next run is going to be late!"
}
}
if ($EndTime -lt (Get-Date)) {
Log ("Script ending at " + (Get-Date))
Break
}
Start-Sleep -Milliseconds $CheckThrottle
}
}
# Do any final once-off completion stuff here
# - EG disconnect from database, send "I'm finished" email
Stop-Transcript