Forum Discussion

John_Dodo's avatar
John_Dodo
Brass Contributor
Dec 07, 2022

exit powershell scriptS when imbricated

Hello,

 

I have a script main.ps1 called with parameters "-environment PRD" or "-environment  "PILOT".

 

This main script calls a toolbox.ps1 script which is like this : 

switch ($environment) {
    "PILOT" {
        #define PILOT vars
    }
    "PRD" {
        #define PRD vars
    }
    default {
        write-host "!!! No Environment provided !! Closing the script."
        exit
    }
}
 
This exit seems to only close the toolbox.ps1 script but not the main.ps1
 
How can I achieve this ?
 
Thank you.
  • AndySvints's avatar
    AndySvints
    Steel Contributor

    Hello John_Dodo,

    One of the quick & dirty and pretty simple option would be to throw an exception.

    Something like this:

    switch ($environment) {
                "PILOT" {
                    #define PILOT vars
                }
                "PRD" {
                    #define PRD vars
                }
                default {
                    throw "!!! No Environment provided !! Closing the script."
                }
            }

    It will generate terminating error and will end you child and main scripts.

     

    Hope that helps.

    Thank you.

     

     

     

     

    • John_Dodo's avatar
      John_Dodo
      Brass Contributor

      Hi AndySvints ,

       

      it seems to work indeed, though it is "quick n dirty" as you mentionned 🙂

       

      I'll use this method for the moment and see if I can find something cleaner in the future.

       

      Thank you.