Accessing non-existent property of a hashtable behaves differently in and out of the module
In a PowerShell script or a simple one-file psm1 module accessing a non-existent property of a hashtable returns $null
$hashtable = @{}
$hashtable.NonExistentKey -eq $null # returns true
But when this code is a part of a proper module with a psd1 manifest the same code throws an exception
The property 'NonExistentKey' cannot be found on this object. Verify that the property exists.
Maybe someone knows what's the reason for this behavior and if it can be changed?
UPD: I know that ContainsKey is the proper way, but it concerns executing legacy code and different behavior in general.
UPD2: Set-StrictMode was indeed the case. Thanks, everyone!
powershell powershell-module
add a comment |
In a PowerShell script or a simple one-file psm1 module accessing a non-existent property of a hashtable returns $null
$hashtable = @{}
$hashtable.NonExistentKey -eq $null # returns true
But when this code is a part of a proper module with a psd1 manifest the same code throws an exception
The property 'NonExistentKey' cannot be found on this object. Verify that the property exists.
Maybe someone knows what's the reason for this behavior and if it can be changed?
UPD: I know that ContainsKey is the proper way, but it concerns executing legacy code and different behavior in general.
UPD2: Set-StrictMode was indeed the case. Thanks, everyone!
powershell powershell-module
5
Set-StrictMode
. The module likely has this active (and that's good practice). Use.ContainsKey
to check if there's a key before accessing it.
– Jeroen Mostert
Nov 26 '18 at 12:35
2
You shouldn't be checking the existence of a key like that anyway. Hashtables have a methodContainsKey()
for this exact purpose.
– Ansgar Wiechers
Nov 26 '18 at 13:05
1
If it's "legacy code", then just turn strict mode off again:Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.
– Jeroen Mostert
Nov 26 '18 at 13:43
add a comment |
In a PowerShell script or a simple one-file psm1 module accessing a non-existent property of a hashtable returns $null
$hashtable = @{}
$hashtable.NonExistentKey -eq $null # returns true
But when this code is a part of a proper module with a psd1 manifest the same code throws an exception
The property 'NonExistentKey' cannot be found on this object. Verify that the property exists.
Maybe someone knows what's the reason for this behavior and if it can be changed?
UPD: I know that ContainsKey is the proper way, but it concerns executing legacy code and different behavior in general.
UPD2: Set-StrictMode was indeed the case. Thanks, everyone!
powershell powershell-module
In a PowerShell script or a simple one-file psm1 module accessing a non-existent property of a hashtable returns $null
$hashtable = @{}
$hashtable.NonExistentKey -eq $null # returns true
But when this code is a part of a proper module with a psd1 manifest the same code throws an exception
The property 'NonExistentKey' cannot be found on this object. Verify that the property exists.
Maybe someone knows what's the reason for this behavior and if it can be changed?
UPD: I know that ContainsKey is the proper way, but it concerns executing legacy code and different behavior in general.
UPD2: Set-StrictMode was indeed the case. Thanks, everyone!
powershell powershell-module
powershell powershell-module
edited Nov 26 '18 at 13:50
aershov
asked Nov 26 '18 at 12:30
aershovaershov
1,49711121
1,49711121
5
Set-StrictMode
. The module likely has this active (and that's good practice). Use.ContainsKey
to check if there's a key before accessing it.
– Jeroen Mostert
Nov 26 '18 at 12:35
2
You shouldn't be checking the existence of a key like that anyway. Hashtables have a methodContainsKey()
for this exact purpose.
– Ansgar Wiechers
Nov 26 '18 at 13:05
1
If it's "legacy code", then just turn strict mode off again:Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.
– Jeroen Mostert
Nov 26 '18 at 13:43
add a comment |
5
Set-StrictMode
. The module likely has this active (and that's good practice). Use.ContainsKey
to check if there's a key before accessing it.
– Jeroen Mostert
Nov 26 '18 at 12:35
2
You shouldn't be checking the existence of a key like that anyway. Hashtables have a methodContainsKey()
for this exact purpose.
– Ansgar Wiechers
Nov 26 '18 at 13:05
1
If it's "legacy code", then just turn strict mode off again:Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.
– Jeroen Mostert
Nov 26 '18 at 13:43
5
5
Set-StrictMode
. The module likely has this active (and that's good practice). Use .ContainsKey
to check if there's a key before accessing it.– Jeroen Mostert
Nov 26 '18 at 12:35
Set-StrictMode
. The module likely has this active (and that's good practice). Use .ContainsKey
to check if there's a key before accessing it.– Jeroen Mostert
Nov 26 '18 at 12:35
2
2
You shouldn't be checking the existence of a key like that anyway. Hashtables have a method
ContainsKey()
for this exact purpose.– Ansgar Wiechers
Nov 26 '18 at 13:05
You shouldn't be checking the existence of a key like that anyway. Hashtables have a method
ContainsKey()
for this exact purpose.– Ansgar Wiechers
Nov 26 '18 at 13:05
1
1
If it's "legacy code", then just turn strict mode off again:
Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.– Jeroen Mostert
Nov 26 '18 at 13:43
If it's "legacy code", then just turn strict mode off again:
Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.– Jeroen Mostert
Nov 26 '18 at 13:43
add a comment |
1 Answer
1
active
oldest
votes
As stated by @Jeroen Mostert above, strict mode might be active.
PowerShell session with active strict mode:
> Set-StrictMode -Version 2.0
> $d = @{}
> $d.SomeNotExistingKey
The property 'SomeNotExistingKey' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $d.SomeNotExistingKey
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) , PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
PowerShell session WITHOUT active strict mode:
> $d = @{}
> $d.SomeNotExistingKey
From MSDN:
... When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
Hope that helps
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%2f53481174%2faccessing-non-existent-property-of-a-hashtable-behaves-differently-in-and-out-of%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
As stated by @Jeroen Mostert above, strict mode might be active.
PowerShell session with active strict mode:
> Set-StrictMode -Version 2.0
> $d = @{}
> $d.SomeNotExistingKey
The property 'SomeNotExistingKey' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $d.SomeNotExistingKey
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) , PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
PowerShell session WITHOUT active strict mode:
> $d = @{}
> $d.SomeNotExistingKey
From MSDN:
... When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
Hope that helps
add a comment |
As stated by @Jeroen Mostert above, strict mode might be active.
PowerShell session with active strict mode:
> Set-StrictMode -Version 2.0
> $d = @{}
> $d.SomeNotExistingKey
The property 'SomeNotExistingKey' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $d.SomeNotExistingKey
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) , PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
PowerShell session WITHOUT active strict mode:
> $d = @{}
> $d.SomeNotExistingKey
From MSDN:
... When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
Hope that helps
add a comment |
As stated by @Jeroen Mostert above, strict mode might be active.
PowerShell session with active strict mode:
> Set-StrictMode -Version 2.0
> $d = @{}
> $d.SomeNotExistingKey
The property 'SomeNotExistingKey' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $d.SomeNotExistingKey
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) , PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
PowerShell session WITHOUT active strict mode:
> $d = @{}
> $d.SomeNotExistingKey
From MSDN:
... When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
Hope that helps
As stated by @Jeroen Mostert above, strict mode might be active.
PowerShell session with active strict mode:
> Set-StrictMode -Version 2.0
> $d = @{}
> $d.SomeNotExistingKey
The property 'SomeNotExistingKey' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $d.SomeNotExistingKey
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) , PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
PowerShell session WITHOUT active strict mode:
> $d = @{}
> $d.SomeNotExistingKey
From MSDN:
... When strict mode is on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block violates basic best-practice coding rules.
Hope that helps
answered Nov 26 '18 at 13:51
MoerwaldMoerwald
2,96041442
2,96041442
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%2f53481174%2faccessing-non-existent-property-of-a-hashtable-behaves-differently-in-and-out-of%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
5
Set-StrictMode
. The module likely has this active (and that's good practice). Use.ContainsKey
to check if there's a key before accessing it.– Jeroen Mostert
Nov 26 '18 at 12:35
2
You shouldn't be checking the existence of a key like that anyway. Hashtables have a method
ContainsKey()
for this exact purpose.– Ansgar Wiechers
Nov 26 '18 at 13:05
1
If it's "legacy code", then just turn strict mode off again:
Set-StrictMode -Off
. Its effects are scoped, so you can selectively apply it where necessary. There's quite a few ways to tweak strict mode and set a default preference, so within a module it's never a good idea to rely on any particular mode being in effect.– Jeroen Mostert
Nov 26 '18 at 13:43