How do I use constants in a PowerShell module?












1















I want to declare some variables as global constants in a module (web service response codes). The problem is, when I import the module (more than once, either with or without -Force) I get



Import-Module : Cannot remove variable ADDED_INCLUSION_STATUS because it is
constant or read-only. If the variable is read-only, try the operation
again specifying the Force option.


What workaround/approach can I use?



My declaration looks like this:



Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -option Constant









share|improve this question

























  • Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

    – boxdog
    Jun 12 '18 at 11:19






  • 2





    I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

    – Clijsters
    Jun 12 '18 at 11:41






  • 1





    How are you declaring your constants?

    – gvee
    Jun 12 '18 at 11:51











  • Remove-Module doesn't help.

    – Max
    Jun 12 '18 at 12:44











  • Check whether the variable exists before trying to (re)define it.

    – Bill_Stewart
    Jun 12 '18 at 16:40
















1















I want to declare some variables as global constants in a module (web service response codes). The problem is, when I import the module (more than once, either with or without -Force) I get



Import-Module : Cannot remove variable ADDED_INCLUSION_STATUS because it is
constant or read-only. If the variable is read-only, try the operation
again specifying the Force option.


What workaround/approach can I use?



My declaration looks like this:



Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -option Constant









share|improve this question

























  • Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

    – boxdog
    Jun 12 '18 at 11:19






  • 2





    I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

    – Clijsters
    Jun 12 '18 at 11:41






  • 1





    How are you declaring your constants?

    – gvee
    Jun 12 '18 at 11:51











  • Remove-Module doesn't help.

    – Max
    Jun 12 '18 at 12:44











  • Check whether the variable exists before trying to (re)define it.

    – Bill_Stewart
    Jun 12 '18 at 16:40














1












1








1


1






I want to declare some variables as global constants in a module (web service response codes). The problem is, when I import the module (more than once, either with or without -Force) I get



Import-Module : Cannot remove variable ADDED_INCLUSION_STATUS because it is
constant or read-only. If the variable is read-only, try the operation
again specifying the Force option.


What workaround/approach can I use?



My declaration looks like this:



Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -option Constant









share|improve this question
















I want to declare some variables as global constants in a module (web service response codes). The problem is, when I import the module (more than once, either with or without -Force) I get



Import-Module : Cannot remove variable ADDED_INCLUSION_STATUS because it is
constant or read-only. If the variable is read-only, try the operation
again specifying the Force option.


What workaround/approach can I use?



My declaration looks like this:



Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -option Constant






powershell variables module






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 20:48









Peter Mortensen

13.6k1984111




13.6k1984111










asked Jun 12 '18 at 11:06









MaxMax

214




214













  • Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

    – boxdog
    Jun 12 '18 at 11:19






  • 2





    I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

    – Clijsters
    Jun 12 '18 at 11:41






  • 1





    How are you declaring your constants?

    – gvee
    Jun 12 '18 at 11:51











  • Remove-Module doesn't help.

    – Max
    Jun 12 '18 at 12:44











  • Check whether the variable exists before trying to (re)define it.

    – Bill_Stewart
    Jun 12 '18 at 16:40



















  • Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

    – boxdog
    Jun 12 '18 at 11:19






  • 2





    I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

    – Clijsters
    Jun 12 '18 at 11:41






  • 1





    How are you declaring your constants?

    – gvee
    Jun 12 '18 at 11:51











  • Remove-Module doesn't help.

    – Max
    Jun 12 '18 at 12:44











  • Check whether the variable exists before trying to (re)define it.

    – Bill_Stewart
    Jun 12 '18 at 16:40

















Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

– boxdog
Jun 12 '18 at 11:19





Did you flag them as Constant or Read-Only? As the error message says, Read-Only variables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if you Remove-Module before re-importing?

– boxdog
Jun 12 '18 at 11:19




2




2





I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

– Clijsters
Jun 12 '18 at 11:41





I like this question and it is not lacking code, as your text describes anything one needs to know to reproduce. However I would really love to see an Minimal, Complete, and Verifiable example which attracts more users to test your case and to troubleshoot your problem

– Clijsters
Jun 12 '18 at 11:41




1




1





How are you declaring your constants?

– gvee
Jun 12 '18 at 11:51





How are you declaring your constants?

– gvee
Jun 12 '18 at 11:51













Remove-Module doesn't help.

– Max
Jun 12 '18 at 12:44





Remove-Module doesn't help.

– Max
Jun 12 '18 at 12:44













Check whether the variable exists before trying to (re)define it.

– Bill_Stewart
Jun 12 '18 at 16:40





Check whether the variable exists before trying to (re)define it.

– Bill_Stewart
Jun 12 '18 at 16:40












1 Answer
1






active

oldest

votes


















0














I think you should change your declaration to:



Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -Option ReadOnly -Force -ErrorAction SilentlyContinue


A ReadOnly variable can be redefined by using the -Force switch. A Constant is there for the duration of the session. These cannot be changed or removed without closing PowerShell.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50815650%2fhow-do-i-use-constants-in-a-powershell-module%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I think you should change your declaration to:



    Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -Option ReadOnly -Force -ErrorAction SilentlyContinue


    A ReadOnly variable can be redefined by using the -Force switch. A Constant is there for the duration of the session. These cannot be changed or removed without closing PowerShell.






    share|improve this answer






























      0














      I think you should change your declaration to:



      Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -Option ReadOnly -Force -ErrorAction SilentlyContinue


      A ReadOnly variable can be redefined by using the -Force switch. A Constant is there for the duration of the session. These cannot be changed or removed without closing PowerShell.






      share|improve this answer




























        0












        0








        0







        I think you should change your declaration to:



        Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -Option ReadOnly -Force -ErrorAction SilentlyContinue


        A ReadOnly variable can be redefined by using the -Force switch. A Constant is there for the duration of the session. These cannot be changed or removed without closing PowerShell.






        share|improve this answer















        I think you should change your declaration to:



        Set-Variable -name STATUS_ABORTED -value 0 -Scope Global -Option ReadOnly -Force -ErrorAction SilentlyContinue


        A ReadOnly variable can be redefined by using the -Force switch. A Constant is there for the duration of the session. These cannot be changed or removed without closing PowerShell.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 20:49









        Peter Mortensen

        13.6k1984111




        13.6k1984111










        answered Jun 12 '18 at 20:26









        TheoTheo

        4,7062520




        4,7062520






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50815650%2fhow-do-i-use-constants-in-a-powershell-module%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Unable to find Lightning Node

            Futebolista