• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

exit out of a PowerShell script in a IF block

K

kghammond

#1
Is there a way to exit out of a PowerShell script inside an IF block
or some other way to use a goto or something like that?

Here is my issue:

If (-not (Test-Path -eq "Myfile"))
{
#Don't do something
#exit/break/goto
}
else
{
# Do your stuff
# 300 lines of code/script here
}

I know I can put everything around a big if block, but I would rather
not do that since it will not be very readalbe. Alternatively, I
could make the main part of my app a big function, but then I have to
pass all my variables to it, or use a bunch of global variables. The
main part uses about 20 variables.

So any thoughts on how to accomplish this task in PowerShell nice and
cleanly?

Thank you
 

My Computer

J

Jacques Barathon [MS]

#2
"kghammond" <kghammond@nrscorp.com> wrote in message
news:1176415685.735956.225530@n76g2000hsh.googlegroups.com...
> Is there a way to exit out of a PowerShell script inside an IF block
> or some other way to use a goto or something like that?
>
> Here is my issue:
>
> If (-not (Test-Path -eq "Myfile"))
> {
> #Don't do something
> #exit/break/goto
> }
> else
> {
> # Do your stuff
> # 300 lines of code/script here
> }


You should be able to use exit if what you want is immediately exit the
script. If what you want is simply exit the existing loop, break is your
friend:

--- test-exit.ps1
param ($max)
$i = 0
do {
if ($i -eq $max) {
exit
}
$i++
} while ($true)
"After loop"
--- end of file

PS> test-exit 10
PS>

--- test-break.ps1
param ($max)
$i = 0
do {
if ($i -eq $max) {
break
}
$i++
} while ($true)
"After loop"
--- end of file

PS> test-break 10
After loop
PS>

Hope that helps,
Jacques
 

My Computer

B

Brandon Shell

#3
Little word of warning here. From my experience, If you use exit in a
function it will exit powershell.

With a little testing of break I notice intresting stuff.
1) If your in a loop it only exits the loop
2) If you not in a loop it breaks script or function/filter

I know there maybe some programming reason why, but logically it would seem
that continue should be used in loops and break should always kill current
scope (for lack of better term.)

Maybe someone should explain the details :)

Here is an example (I tried to catch all the options, but im sure I might
have missed some)

function foo{
Param([switch]$iBreak)
Begin{
function foofoo{
param($i)
Write-Host "Started FooFoo $i"
if($i -eq 4)
{
Write-Host "Break in foofoo $i"
Break
}
Write-Host "Hmmm... didnt break foofoo $i"
}
write-Host "Started Foo"
if($iBreak)
{
Write-Host "Arg passed Breaking in Foo"
Break
}
$i = 0
While($true)
{
Write-Host $i
if($i -eq 5){break}
$i++
}
}
Process{
if($_){foofoo $_}
}
End{
Write-Host "No argument Passed or Failed to Break"
foofoo
write-Host "End of Foo"
}
}
"Jacques Barathon [MS]" <jbaratho@online.microsoft.com> wrote in message
news:O2xn8MbfHHA.928@TK2MSFTNGP02.phx.gbl...
> "kghammond" <kghammond@nrscorp.com> wrote in message
> news:1176415685.735956.225530@n76g2000hsh.googlegroups.com...
>> Is there a way to exit out of a PowerShell script inside an IF block
>> or some other way to use a goto or something like that?
>>
>> Here is my issue:
>>
>> If (-not (Test-Path -eq "Myfile"))
>> {
>> #Don't do something
>> #exit/break/goto
>> }
>> else
>> {
>> # Do your stuff
>> # 300 lines of code/script here
>> }

>
> You should be able to use exit if what you want is immediately exit the
> script. If what you want is simply exit the existing loop, break is your
> friend:
>
> --- test-exit.ps1
> param ($max)
> $i = 0
> do {
> if ($i -eq $max) {
> exit
> }
> $i++
> } while ($true)
> "After loop"
> --- end of file
>
> PS> test-exit 10
> PS>
>
> --- test-break.ps1
> param ($max)
> $i = 0
> do {
> if ($i -eq $max) {
> break
> }
> $i++
> } while ($true)
> "After loop"
> --- end of file
>
> PS> test-break 10
> After loop
> PS>
>
> Hope that helps,
> Jacques
>
>
 

My Computer

J

Jacques Barathon [MS]

#4
"Brandon Shell" <tshell.mask@mk.gmail.com> wrote in message
news:uLeTodcfHHA.1240@TK2MSFTNGP04.phx.gbl...
> Little word of warning here. From my experience, If you use exit in a
> function it will exit powershell.


You're right, I should have mentioned that. But the question was
specifically on how to exit out of a script, hence my straight answer.

Jacques
 

My Computer

K

kghammond

#5
On Apr 13, 11:22 am, "Jacques Barathon [MS]"
<jbara...@online.microsoft.com> wrote:
> "Brandon Shell" <tshell.m...@mk.gmail.com> wrote in message
>
> news:uLeTodcfHHA.1240@TK2MSFTNGP04.phx.gbl...
>
> > Little word of warning here. From my experience, If you use exit in a
> > function it will exit powershell.

>
> You're right, I should have mentioned that. But the question was
> specifically on how to exit out of a script, hence my straight answer.
>
> Jacques


So it seems break is the best option for me. I just break will break
you out of a loop or if you are not in a loop, it will break you out
of the function or the main script if you are not in a function.

I am sure there is probalby a better way to put this, but break was
definately my friend for what i was trying to do.

Thank You
 

My Computer

S

steven.hystad@gmail.com

#6
On Apr 12, 4:08 pm, "kghammond" <kghamm...@nrscorp.com> wrote:
> Is there a way to exit out of a PowerShell script inside an IF block
> or some other way to use a goto or something like that?
>
> Here is my issue:
>
> If (-not (Test-Path -eq "Myfile"))
> {
> #Don't do something
> #exit/break/goto}
>
> else
> {
> # Do your stuff
> # 300 lines of code/script here
>
> }
>
> I know I can put everything around a big if block, but I would rather
> not do that since it will not be very readalbe. Alternatively, I
> could make the main part of my app a big function, but then I have to
> pass all my variables to it, or use a bunch of global variables. The
> main part uses about 20 variables.
>
> So any thoughts on how to accomplish this task in PowerShell nice and
> cleanly?
>
> Thank you


return
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)