I have the following PowerShell loop:
Set-Variable -Name YELLOW -Option ReadOnly -Value 0
Set-Variable -Name ORANGE -Option ReadOnly -Value 1
Set-Variable -Name RED -Option ReadOnly -Value 2
While ($true) {
for ($color = $YELLOW; $color -le $RED; $color++) {
$LastWriteTime = ((Get-Item $FilePath).LastWriteTime)
$waitTime = (Get-Date).AddMinutes(-($WarningTimeArray[$color]))
$SleepTime = ($LastWriteTime - $waitTime).TotalSeconds
If ($SleepTime -gt 0) {
$WarningFlagArray[$color] = $false;
SendMail ("Start-Sleep -Seconds" + $SleepTime)
Start-Sleep -Seconds $SleepTime
Continue #Actually want a "redo"
}
ElseIf (-not $WarningFlagArray[$color]) {
SendMail $WarningMessageArray[$color]
$WarningFlagArray[$color] = $true
}
}
}
When PowerShell hits the “Continue”, it continues back to the top of the for loop, and goes to the next color.
In Perl, you have last (equivalent to PowerShell’s break) and you have next (equivalent to PowerShell’s continue). You also have the redo which goes back to the top of the loop, but doesn’t increment the for loop counter.
Is there an equivalent Powershell command to Perl’s redo function for the for loop?
I can easily convert the statement into a while loop to get around this particular issue, but I was wondering if there is something like redo anyway.
I’m pretty sure PowerShell doesn’t support what you’re looking for. You can have labelled break and continue statements but neither will take you to the top of the inside of the associated loop/switch construct. If PowerShell had a goto statment you’d be set but I’m glad it doesn’t have that keyword. 🙂 For more info on labelled break/continue see the about_break help topic.