Afternoon all,
I've got a script that runs scheduled tasks on some remote computers through Cimsessions.
Start-ScheduledTask -CimSession $CimSessions -TaskName "<Task-Name>"
I then have a timer that runs in a Do/Until loop until the tasks are completed. However, the loop ends when one server has completed the task even if others have finished. Is there a way I can re-write my loop to continue until all servers have registered that their task is not running
$StartTime = Get-Date
Do{
$ElapsedTime = (Get-Date) - $StartTime
$TotalTime = "{0:HH:mm:ss}" -f ([datetime]$ElapsedTime.Ticks)
$CurrentLine = $host.UI.RawUI.CursorPosition
Write-Output "Running Scheduled Task... [Total Elapsed Time: $(stc $TotalTime yellow)]"
sleep -s 2
$host.UI.RawUI.CursorPosition = $CurrentLine
}Until((Get-ScheduledTask -CimSession $CimSessions -TaskName "<Task-Name").State -ne 'Running')
Note: the line of code $(stc $TotalTime yellow) is just a custom function that changes the color of the text to yellow
The condition:
Is saying, "run this loop and stop as soon as there is one object with it's
Stateproperty not equal toRunning". What you want instead is, "run this loop while there are objects having theStateproperty equal toRunning", hence the condition should be:As aside, you could greatly simplify the task of
$ElapsedTime = (Get-Date) - $StartTimeby leveraging theStopWatchClass. Here is a little example: