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 :)

2
  • 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. Commented Nov 3, 2016 at 18:02
  • 1
    Possible duplicate of Jumping around to certain spots in script Commented Nov 3, 2016 at 21:37

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Looks like 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.
1

Simply wrap all your code in another loop.

DO {

    #your code here#

} WHILE ($true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.