Loop Function Until Certain Time

From psadt | Evergreen Application Management
Jump to navigation Jump to search



Code from Sion IT


#setup loop
$TimeStart = Get-Date
$TimeEnd = $timeStart.addminutes(2)
Write-Host "Start Time: $TimeStart"
write-host "End Time: $TimeEnd"
Do {
    $TimeNow = Get-Date
    If ($TimeNow -ge $TimeEnd)
        Write-host "It's time to finish."
        Write-Host "Not done yet, it's only $TimeNow"
    Start-Sleep -Seconds 5
Until ($TimeNow -ge $TimeEnd)

The script waits for 10 seconds before looping, this is to stop it burning up processing resources for no good reason.

You can change the .addminutes line to be .addhours, .adddays, .addseconds etc. and it will work in the same way.