Windows Vista Forums

exit out of a PowerShell script in a IF block

  1. #1


    kghammond Guest

    exit out of a PowerShell script in a IF block

    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 System SpecsSystem Spec

  2.   


  3. #2


    Jacques Barathon [MS] Guest

    Re: exit out of a PowerShell script in a IF block

    "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 System SpecsSystem Spec

  4. #3


    Brandon Shell Guest

    Re: exit out of a PowerShell script in a IF block

    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 System SpecsSystem Spec

  5. #4


    Jacques Barathon [MS] Guest

    Re: exit out of a PowerShell script in a IF block

    "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 System SpecsSystem Spec

  6. #5


    kghammond Guest

    Re: exit out of a PowerShell script in a IF block

    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 System SpecsSystem Spec

  7. #6


    steven.hystad@gmail.com Guest

    Re: exit out of a PowerShell script in a IF block

    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 System SpecsSystem Spec


exit out of a PowerShell script in a IF block
Similar Threads
Thread Forum
read powershell exit code from batch PowerShell
How to exit script? PowerShell
A script to block a port in a P.C,? PowerShell
Obtaining Script Exit Return Codes PowerShell
COM objects and PowerShell process exit PowerShell