Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Created July 30, 2023 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericwastaken/81338286f31e47e37ed36bde8496195b to your computer and use it in GitHub Desktop.
Save ericwastaken/81338286f31e47e37ed36bde8496195b to your computer and use it in GitHub Desktop.
Powershell run script at interval
######################################################################
# Runs another powershell script at a given interval.
# Syntax:
# .\run-script-at-interval.ps1 [script path] [interval in seconds]
# Example:
# .\run-script-at-interval.ps1 .\script-to-run.ps1 60
######################################################################
param (
[string]$ScriptPath,
[int]$RepeatIntervalSeconds
)
# Validate the provided script path
if (-not (Test-Path $ScriptPath -PathType Leaf)) {
Write-Host "Invalid script path: $ScriptPath"
exit 1
}
# Validate the repeat interval seconds
if ($RepeatIntervalSeconds -lt 1) {
Write-Host "Invalid repeat interval: $RepeatIntervalSeconds seconds. The interval must be at least 1 second."
exit 1
}
# Display the script path and interval
Write-Host "Running script '$ScriptPath' every $RepeatIntervalSeconds seconds."
# Infinite loop to run the script every specified interval
while ($true) {
# Run the script using the call operator &
& $ScriptPath
# Output a single dot without a newline character
Write-Host -NoNewline "."
# Wait for the specified interval before running the script again
Start-Sleep -Seconds $RepeatIntervalSeconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment