0

I have a script that is being called via a Windows Scheduled Task, and that task is triggered based on a certain Windows Application Event. It is only critical to execute the script, though, if the event occurs 3 or more times in 1 minute; if the event occurs once a minute, no action should be taken.

I know this can be handled in the script itself. Let's say there are at least 2 new variables I will need:

# time window, in seconds
$maxTime = 60

# max number of times this script needs to be called, within $maxTime window, 
# before executing the rest of the script
$maxCount = 3  

I started outlining an algorithm using a temp file as tracking, but thought there might be a simpler solution that someone can show me. Thanks

2 Answers 2

1

You could store your execution times in an environment variable.

Before this script will work, you must create the LastExecutionTimes environment variable.

$maxTime = 60
$maxCount = 3
$now = Get-Date

# Get execution times within the time limit.
$times = @($env:LastExecutionTimes -split ';'| 
            Where-Object {$_ -and $now.AddSeconds(-1 * $maxTime) -lt $_})

$times += '{0:yyyy-MM-dd HH:mm:ss}' -f $now
$env:LastExecutionTimes = $times -join ';'

if($times.Length -lt $maxCount) {return}

# Reset the execution times
$env:LastExecutionTimes =''

Write-Host 'Continue Script' -ForegroundColor Yellow
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, I was having trouble getting the environment variable to save and carry over to the next time the script was called (new powershell instance). So I updated the script to use an alternative get/set method: $envTimes = [Environment]::GetEnvironmentVariable("LastExecutionTimes", "User") and [Environment]::SetEnvironmentVariable("LastExecutionTimes", "$envTimes", "User") This worked, and also, did not require adding the variable beforehand.
0

I would write a text file and a secondary script or function to check it. Where essentially it will call it each time, and then writes the information writes to a text file at call time.

The something like this:

if(!((Get-Date).AddMinutes(-1) -lt $oldTime))
 {
    $CurDate = Get-Date
    "$CurDate, 1" | out-File "TheCheck.txt"
 }
 else 
 {
  $counter++
  if($counter -ge 3) {Call WorkerFunction}
   else{
    "$oldTime, $counter" | Out-File "TheCheck.txt"
 }

Its missing some variables, but overall should be functional as a supplemental script. Then what your scheduled task actually does is call this, if the time since the $oldTime is over 1 minute, then it over writes the file with the current time and 1 for a $counter variable. If its less than a minute since the first call it then checks the $counter and if it is 3 or higher (could also do -eq ) to 3 then it calls your main script.

1 Comment

The check for 3 or higher should be $counter -ge 3 and the $counter should be increased before the check since there is a new event that caused the script to run. Even then storing only one time won't work. If 3 events were each 40 seconds apart, the counter would be increased to 3, but the time from first event to third event would be 1 minute 20 seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.