Created
July 30, 2023 04:39
-
-
Save ericwastaken/81338286f31e47e37ed36bde8496195b to your computer and use it in GitHub Desktop.
Powershell run script at interval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################################################################### | |
# 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