不能在空值表达式上调用方法 [英] You cannot call a method on a null-valued expression

查看:37
本文介绍了不能在空值表达式上调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想创建一个 powershell 脚本来计算可执行文件(文件)的 md5 总和.

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

我的 .ps1 脚本:

My .ps1 script:

$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

据我所知,这个错误意味着脚本正在尝试做某事,但脚本的另一部分没有任何信息来允许脚本的第一部分正常工作.在这种情况下,$hash.

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.

Get-ExecutionPolicy 输出 Unrestricted.

导致此错误的原因是什么?
我的空值表达式究竟是什么?

感谢任何帮助.如果这是微不足道的,我深表歉意,并将继续我的研究.

Any help is appreciated. I apologize if this is trivial and will continue my research.

参考资料:

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

如何在 PowerShell 中获取 MD5 校验和

推荐答案

这个问题的简单答案是您有一个未声明的 (null) 变量.在这种情况下,它是 $md5.根据您的评论,这需要在代码的其他地方声明

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

$md5.ComputeHash().ComputeHash() 是空值表达式.输入乱码也会产生同样的效果.

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 默认允许按照其定义的方式发生这种情况 严格模式

Set-StrictMode 关闭时,未初始化的变量(版本 1)被假定为具有 0(零)或 $Null 的值,具体取决于类型.对不存在的属性的引用返回 $Null,无效的函数语法的结果因错误而异.不允许使用未命名的变量.

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.

这篇关于不能在空值表达式上调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆