2016-11-03 1 views
0

Ich bin neu bei Powershell und ich brauche etwas Hilfe, um meinen Code zu beenden. Alles ist in Ordnung, aber ich brauche auf ELSE gehen Sie zurück, um eine Schleife zu tun. Wie kann ich das lösen?Powershell Do Loop

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

+1

Kurze Antwort: unmöglich. Powershell kann nicht zwischen den Zeilen springen wie VBS. Sie müssen einen Weg finden, Ihre Bedürfnisse in Ihrem Do-while-Ausdruck zu erfüllen. – restless1987

+1

Mögliches Duplikat von [Springen zu bestimmten Stellen im Skript] (http://stackoverflow.com/questions/20221565/jumping-around-to-certain-spots-in-script) – TessellatingHeckler

Antwort

1

mit rekursive Methode

 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 
+1

Die Verwendung von Rekursion ist clever. Man muss sicherstellen, dass der Stapel nicht zu tief wird, und ich weiß nicht, wie tief das ist. –

+2

[Sieht so aus] (http://stackoverflow.com/questions/10755699/how-can-i-configure-call-depth-in-powershell) PSv3 und höher haben ein dynamisches Limit für die Anruftiefe, unterstützen aber kein Tail Anrufoptimierung (zumindest PSv4 nicht). Ein einfacher Test auf meinem Computer löst eine 'Call Depth Overflow' Ausnahme nach ~ 5k Calls aus, so dass diese Antwort irgendwann aufhört zu loopen und zu brechen. – TessellatingHeckler

+0

hat gearbeitet: D danke für die Hilfe m8 – emnt

1

einfach wickeln den gesamten Code in einer anderen Schleife.

DO { 

    #your code here# 

} WHILE ($true)