Home > Software design >  how to run a command x number of times inside a while loop - powershell
how to run a command x number of times inside a while loop - powershell

Time:10-26

so here i have a part of my code where im trying to work out how i can run a command inside a while loop x number of times and end the script.

$timer = New-TimeSpan -Minutes 120

$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timer) {  
    $ReleaseStatus = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/company/Project/_apis/release/releases/$RId/environments/$EId/?`api-version=6.0" -Method GET -Headers $Header -Verbose
    start-sleep -seconds 10
    #if release is successful return message and end script. 
    if ($ReleaseStatus.Status -eq 'Succeeded') {
        write-host "Release Succeeded"
        return
    }
        
    #if the release fails run the command x number of times. 
    if ($ReleaseStatus.status -eq 'Rejected') {
        Write-Host "Release failed, Re-running release for you"
        #input command here to run release x number of times and then end whole script.
    }
}

I have tried various methods but not getting anywhere, anyone know how i can perform this?

CodePudding user response:

Use a variable to keep count of your retries:

$timer = New-TimeSpan -Minutes 120

# configure number of attempts - this will run up to 5 times (initial attempt   4 retries)
$retries = 5

$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timer -and $retries -gt 0) {
    $ReleaseStatus = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/company/Project/_apis/release/releases/$RId/environments/$EId/?`api-version=6.0" -Method GET -Headers $Header -Verbose
    start-sleep -seconds 10

    #if release is successful return message and end script. 
    if ($ReleaseStatus.Status -eq 'Succeeded') {
        write-host "Release Succeeded"
        return
    }
    

    # release didn't succeed, decrement retry counter
    $retries--

    if($retries -ge 1){
        Write-Host "Release failed, Re-running release for you"
    }
    else {
        Write-Host "Release failed, but no more retries for you"
    }
}

Now the while() condition only continues if 1) we haven't timed out yet AND 2) we have retries left - if either condition is no longer met it'll stop

CodePudding user response:

okay so here's how i did it. please read the hash notes.

#Check the status of release every y seconds 

$timer = New-TimeSpan -Minutes 120
#set the value of I to 0 outside of the while loop.
$i = 0
$sw = [diagnostics.stopwatch]::StartNew()
while ($sw.elapsed -lt $timer) {  
    $ReleaseStatus  = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/$Organisation/$Project/_apis/release/releases/$RId/environments/$EId/?`api-version=6.0" -Method GET -Headers $Header -Verbose
    start-sleep -seconds 10
    
    if ($ReleaseStatus.Status -eq 'Succeeded') {
        write-host "Release Succeeded"
        return
    }
    if ($ReleaseStatus.status -eq 'Rejected') {
        Write-Host "Release failed, Re-running release for you"

        $Uri = "https://vsrm.dev.azure.com/$Organisation/$Project/_apis/Release/releases/$RId/environments/$EId/?api-version=6.0-preview.6"
         #we've put the value of retries into a variable to be parametrised. If I is greater than number of retries then return(cancel operation)
        if ($i -gt $NumberOfRetryAttempts) {
            return
        }

        Invoke-RestMethod -uri $uri -Method PATCH -Headers $Header -Body $body2 
        #you put the I variable inside the while loop and you increment by 1 each time the condition comes back false.
        $i   
    }
}
  • Related