RelaxDK
RelaxDK

Reputation: 89

An elegant way to get functions to run at specific time

I have a script with several functions that has to be executed at specific timeslots. Function A needs to run every 15 sec. Function B needs to run every minute. Im doing this in an endless loop.

While ($true){
A
start-sleep -seconds 15
A
start-sleep -seconds 15
A
start-sleep -seconds 15
A
start-sleep -seconds 15
B
}

Now I need to start another function (C) every 10 sec. This function is time consuming and My endless loop will move in the time slot.

I have an idea of executing the functions as jobs in parralell. But how do I trig the job start at the right time with the right pause?

Thanx in advance.

Upvotes: 1

Views: 1425

Answers (1)

Alex_P
Alex_P

Reputation: 2950

I created this code with a mixture from Microsoft - Register-ObjectEvent and Mitzen.Blogspot.

$timer = New-Object -TypeName System.Timers.Timer    
$Timer.Interval = 15000
$Timer.Autoreset = $True
$Timer.Enabled = $True
$timer.Start()

$objectEventArgs = @{
    InputObject = $Timer
    EventName = 'Elapsed'
    SourceIdentifier = 'Timer.Elapsed'
}
$action = {
    Write-Host -Object "Enter your function in this script block."
}

Register-ObjectEvent @objectEventArgs -Action $action 

Upvotes: 2

Related Questions