简体   繁体   English

不能在空值表达式上调用方法

[英]You cannot call a method on a null-valued expression

I am simply trying to create a powershell script which calculates the md5 sum of an executable (a file).我只是想创建一个计算可执行文件(文件)的 md5 总和的 powershell 脚本。

My .ps1 script:我的 .ps1 脚本:

$answer = Read-Host "File name and extension (ie; file.exe)"
$someFilePath = "C:\Users\xxx\Downloads\$answer"

If (Test-Path $someFilePath){
                        $stream = [System.IO.File]::Open("$someFilePath",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
                        $hash = [System.BitConverter]::ToString($md5.ComputeHash($stream))
                        $hash
                        $stream.Close()
                        }
Else{
Write-Host "Sorry, file $answer doesn't seem to exist."
}

Upon running my script I receive the following error:运行我的脚本后,我收到以下错误:

You cannot call a method on a null-valued expression.
At C:\Users\xxx\Downloads\md5sum.ps1:6 char:29
+                             $hash = [System.BitConverter]::ToString($md5.Compute ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

To my understanding, this error means the script is attempting to do something, but another part of the script does not have any information to permit the first part of the script to work properly.据我了解,此错误意味着脚本正在尝试执行某些操作,但脚本的另一部分没有任何信息来允许脚本的第一部分正常工作。 In this case, $hash .在这种情况下, $hash

Get-ExecutionPolicy outputs Unrestricted . Get-ExecutionPolicy输出Unrestricted

What is causing this error?是什么导致了这个错误?
What exactly is my null valued expression?我的空值表达式究竟是什么?

Any help is appreciated.任何帮助表示赞赏。 I apologize if this is trivial and will continue my research.如果这是微不足道的,我深表歉意,并将继续我的研究。


References:参考:

http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/27/troubleshoot-the-invokemethodonnull-error-with-powershell.aspx http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/27/troubleshoot-the-invokemethodonnull-error-with-powershell.aspx

How to get an MD5 checksum in PowerShell 如何在 PowerShell 中获取 MD5 校验和

The simple answer for this one is that you have an undeclared (null) variable.这个问题的简单答案是您有一个未声明的(空)变量。 In this case it is $md5 .在这种情况下,它是$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..ComputeHash()$md5.ComputeHash()为空值表达式。 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默认情况下,PowerShell 允许按照其StrictMode 的定义发生这种情况

When Set-StrictMode is off, uninitialized variables (Version 1) are assumed to have a value of 0 (zero) or $Null, depending on type.Set-StrictMode关闭时,未初始化的变量(版本 1)被假定为具有 0(零)或 $Null 的值,具体取决于类型。 References to non-existent properties return $Null, and the results of function syntax that is not valid vary with the error.对不存在的属性的引用返回 $Null,无效的函数语法的结果因错误而异。 Unnamed variables are not permitted.不允许使用未命名的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM