You cannot call a method on a null-valued expression

后端 未结 1 425
礼貌的吻别
礼貌的吻别 2020-12-03 17:18

I am simply trying to create a powershell script which calculates the md5 sum of an executable (a file).

My .ps1 script:

$answer = Read-Host \"File n         


        
相关标签:
1条回答
  • 2020-12-03 17:52

    The simple answer for this one is that you have an undeclared (null) variable. In this case it is $md5. From the comment you put this needed to be declared elsewhere in your code

    $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    

    The error was because you are trying to execute a method that does not exist.

    PS C:\Users\Matt> $md5 | gm
    
    
       TypeName: System.Security.Cryptography.MD5CryptoServiceProvider
    
    Name                       MemberType Definition                                                                                                                            
    ----                       ---------- ----------                                                                                                                            
    Clear                      Method     void Clear()                                                                                                                          
    ComputeHash                Method     byte[] ComputeHash(System.IO.Stream inputStream), byte[] ComputeHash(byte[] buffer), byte[] ComputeHash(byte[] buffer, int offset, ...
    

    The .ComputeHash() of $md5.ComputeHash() was the null valued expression. Typing in gibberish would create the same effect.

    PS C:\Users\Matt> $bagel.MakeMeABagel()
    You cannot call a method on a null-valued expression.
    At line:1 char:1
    + $bagel.MakeMeABagel()
    + ~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull
    

    PowerShell by default allows this to happen as defined its StrictMode

    When Set-StrictMode is off, uninitialized variables (Version 1) are assumed to have a value of 0 (zero) or $Null, depending on type. References to non-existent properties return $Null, and the results of function syntax that is not valid vary with the error. Unnamed variables are not permitted.

    0 讨论(0)
提交回复
热议问题