Announcement

Collapse
No announcement yet.

A do-while error powershell script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • A do-while error powershell script

    Hi,

    need you help here

    I have a Script in Powershell that runs from command line, I would like to run it in a Do While loop until error code is 0

    Code:
     
    powershell.exe -Command "& {$host.UI.RawUI.WindowTitle='TASK: RunningAScript'; Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force; \\blabla\Scripts\RunningAscript.ps1 -Daily -maxCount:5000 -Automation; $ret=$LASTEXITCODE; exit $ret }"  -WindowStyle Minimized -NonInteractive -ExecutionPolicy Unrestricted
    how do I add the Do-While loop into this command so it will run again and again until the error code will be 0 (which means the script didn't fail)

    Thanks !

  • #2
    Re: A do-while error powershell script

    is it something like this:

    do { powershell...... } while %errorlevel% <>0

    ???

    thanks

    Comment


    • #3
      Re: A do-while error powershell script

      Originally posted by XtaZee View Post
      is it something like this:

      do { powershell...... } while %errorlevel% <>0

      ???

      thanks
      you can try this
      http://www.powershellpro.com/powersh...c-using-loops/

      Code:
      $i = 1; while ($i -le 5) {Write-Host $i; $i++}

      /Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: A do-while error powershell script

        Originally posted by Rems View Post
        you can try this
        http://www.powershellpro.com/powersh...c-using-loops/

        Code:
        $i = 1; while ($i -le 5) {Write-Host $i; $i++}

        /Rems

        That actually helped me I did something like this:

        Code:
         do {blablablabla} while ( $LASTEXITCODE -ne 0)
        to finish this I need to add a try catch - so it will catch the error sleep and retry... pointers?

        Comment


        • #5
          Re: A do-while error powershell script

          Maybe like
          Code:
          do {blablablabla} while (!$?)
          Where $? is a variable you can sometimes use to determine whether the last command was successful or not, it can be equal to $true or $false
          The exclamation mark means NOT ("do while not successful" )

          /Rems
          Last edited by Rems; 17th July 2014, 18:05.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment

          Working...
          X