I have a script of counter that expects two parameters:
1) seconds to wait before the counter starts.
2) counter duration in seconds.
For example, if I input 3,10
I would like that after 3 seconds the timer will countdown from 10 to 0 and write it to the output every second.
This is my script:
$timeBeforeStart = $args[0]
$waitSeconds = $args[1]
$startTime = get-date
$endTime = $startTime.addSeconds($waitSeconds)
$timeSpan = new-timespan $startTime $endTime
start-sleep -s $timeBeforeStart
while ($timeSpan -gt 0)
{
$timeSpan = new-timespan $(get-date) $endTime
write-host $([string]::Format("`rTime Remaining: {0:d2}:{1:d2}:{2:d2}",
$timeSpan.hours, $timeSpan.minutes, $timeSpan.seconds))
sleep 1
}
Unfortunately it doesn’t work, the sleep seems to work simultaneity with the counter instead of delay the counter.
PS C:\> c:\555.ps1 3 10
Time Remaining: 00:00:07
Time Remaining: 00:00:05
Time Remaining: 00:00:04
Time Remaining: 00:00:03
Time Remaining: 00:00:02
Time Remaining: 00:00:01
Time Remaining: 00:00:00
Time Remaining: 00:00:00
I’ve also tried start-sleep -s and the results where the same.
by the way, what is the difference between sleep and “start-sleep -s” ?
No difference,
sleepis just alias ofStart-Sleep.You might want to put the sleep before the counter starts before getting the first
$timeSpan: