Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, February 20, 2016 3:53 PM
Hi
I'm working on a script and it involves multiple loops. You start with one but its not long before you need another for another task then another.
I started of with a Do While loop then there are many inside this.
I need at some parts of the script to choose to goto the beginning of the script again or to goto back to certain points of the script.
Like if $answer = x goto :start
Ive searched about and read this is not good practice but haven't found a non vague answer as to what to do instead.
Here's hoping
Thanks for reading
Confuseis
confuseis
All replies (7)
Saturday, February 20, 2016 11:09 PM ✅Answered | 1 vote
You can only do this with labeled loops. If you place the whole script in a loop you can just jump t the outermost loop.
:start do{
:next do{
:inner do{
...
break start
https://technet.microsoft.com/en-us/library/hh847873.aspx?f=255&MSPPError=-2147217396
\(ツ)_/
Saturday, February 20, 2016 8:17 PM
loops can contain labels. An inner loop can jump to a previously labeled outer loop.
Most of your issue is related to design. Without a concrete example it is not really possible to give you much better advice.
\(ツ)_/
Saturday, February 20, 2016 9:52 PM
Presently at the end of the script I have a switch condition/loop
In the example if the user choose Option a, b or none listed then I want to break back to the beginning of the loop. if user chooses option c I would want to goto the very start of the script defined by :StartOfScript
:ChooseOption switch ($readhost)
a { ipconfig /all; Break Chooseoption }
b { get-childitem *.exe; Continue ChooseOption }
c { "re run script"; Break StartOfScript }
Default { Wrong Button; continue ChooseOption }
confuseis
Sunday, February 21, 2016 12:11 PM
Got some progress. Thanks
when I use break on its own it will under one occasion (option c) it will break to the beginning. Below would be an example of the last loop in the script.
only option c will work and it goes back to the beginning as intended.
Idea here is to run the command to list e.g. printers then pause so the user can read the list.
When button is pressed then break to the beginning again however in practice it will just exit the script rather than break to the beginning.
I noticed that the break command would work for option c but not break start.
The Default option will output the text then just exit the script and not break to the beginning
:Start Do
{
$readhost = read-host "Choose A, B or C"
switch ($readhost)
{
a { get-wmiobject -computername comp1 -class win32_printer; break start }
b {get-wmiobject -computername comp1 -class win32_printer; cmd /c pause |out-null; break}
c { "re run script"; Break }
Default { "Wrong Button"; break }
}
}
While {read-host -eq "c" }
confuseis
Sunday, February 21, 2016 1:07 PM
Go back and read the help again. You have not understood what it is showing you.
You cannot break to the loop you are in.
\(ツ)_/
Sunday, February 21, 2016 1:12 PM
Ive edited my example. it was actually a loop inside a loop. the whole script is inside the do loop. The section in the example is just the last part of the script.
confuseis
Sunday, February 21, 2016 1:21 PM
It is still only one loop.
Try formatting your example and posting in a code box so you cab actually see what you have.
\(ツ)_/