How do I use constants in a PowerShell module?
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
add a comment |
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
Did you flag them asConstantorRead-Only? As the error message says,Read-Onlyvariables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if youRemove-Modulebefore 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-Moduledoesn'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
add a comment |
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
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
powershell variables module
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 asConstantorRead-Only? As the error message says,Read-Onlyvariables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if youRemove-Modulebefore 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-Moduledoesn'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
add a comment |
Did you flag them asConstantorRead-Only? As the error message says,Read-Onlyvariables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if youRemove-Modulebefore 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-Moduledoesn'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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
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
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Did you flag them as
ConstantorRead-Only? As the error message says,Read-Onlyvariables can be forcibly removed (though, I'm not sure PowerShell will do this for you behind the scenes). Alternatively, does it help if youRemove-Modulebefore 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-Moduledoesn'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