Accessing non-existent property of a hashtable behaves differently in and out of the module












0















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!










share|improve this question




















  • 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


















0















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!










share|improve this question




















  • 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
















0












0








0








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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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










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














1 Answer
1






active

oldest

votes


















1














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






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%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









    1














    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






    share|improve this answer




























      1














      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






      share|improve this answer


























        1












        1








        1







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 13:51









        MoerwaldMoerwald

        2,96041442




        2,96041442
































            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%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





















































            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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin