0

I'm new at powershell and i need some help to end my code. Everything is ok but i need on ELSE go back to DO doing a loop. How can i solve this?

  DO {
      $getinfo=Get-Content $wblogpath | Select-Object -last 1 
      Write-Host -NoNewline "."$counter
      Start-Sleep -s 1
      $counter++
     }
  WHILE ($getinfo -notlike '*ReadyToRun->Run*' -and 
         $getinfo -notlike '*test*' -and
         $getinfo -notlike '*error*' -and
         $counter -le 8)

  IF ($getinfo -like '*ReadyToRun->Run*')
      {
       Write-Host "`nREADY TO RUN"
      }
  ELSEIF ($getinfo -like '*test*') 
      {
       Write-Host "`ntest"
      }
  ELSEIF ($getinfo -like '*error*') 
      {
       Write-Host "`nerror"
      }
  ELSE{
       Write-Host "`nRESTARTing WINBASIS"
       $counter=0
      }

Thanks :)

emnt
  • 25
  • 7
  • 1
    Short answer: impossible. Powershell can not jump between lines like vbs does. You must find a way to achive your needs in your do-while-expression. – restless1987 Nov 03 '16 at 18:02
  • 1
    Possible duplicate of [Jumping around to certain spots in script](http://stackoverflow.com/questions/20221565/jumping-around-to-certain-spots-in-script) – TessellatingHeckler Nov 03 '16 at 21:37

2 Answers2

1

with recursive method

     function myfunction()
     {
     DO {
           $wblogpath="C:\temp\test.csv"
           $getinfo=Get-Content $wblogpath | Select-Object -last 1 
           Write-Host -NoNewline "."$counter
           Start-Sleep -s 1
           $counter++
          }
       WHILE ($getinfo -notlike '*ReadyToRun->Run*' -and 
              $getinfo -notlike '*test*' -and
              $getinfo -notlike '*error*' -and
              $counter -le 8)

       IF ($getinfo -like '*ReadyToRun->Run*')
           {
            Write-Host "`nREADY TO RUN"
           }
       ELSEIF ($getinfo -like '*test*') 
           {
            Write-Host "`ntest"
           }
       ELSEIF ($getinfo -like '*error*') 
           {
            Write-Host "`nerror"
           }
       ELSE{
            Write-Host "`nRESTARTing WINBASIS"
            $counter=0
            myfunction
           }
     }

     myfunction
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • 1
    The use of recursion is clever. One has to make sure the stack doesn't get too deep, and I don't know how deep that is. – Walter Mitty Nov 03 '16 at 19:26
  • 2
    [Looks like](http://stackoverflow.com/questions/10755699/how-can-i-configure-call-depth-in-powershell) PSv3 and above have a dynamic call depth limit, but don't support tail call optimization (at least PSv4 doesn't). A simple test on my computer throws a 'call depth overflow' exception after ~5k calls, so this answer will - at some point - stop looping and break. – TessellatingHeckler Nov 03 '16 at 20:31
1

Simply wrap all your code in another loop.

DO {

    #your code here#

} WHILE ($true)
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87