【问题标题】:You cannot call a method on a null-valued expression您不能在空值表达式上调用方法
【发布时间】:2015-02-15 23:26:15
【问题描述】:

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

我的 .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."
}

在运行我的脚本时,我收到以下错误:

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

Get-ExecutionPolicy 输出Unrestricted

是什么导致了这个错误?
我的空值表达式到底是什么?

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


参考文献:

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

【问题讨论】:

  • 什么是$md5?该变量不在您显示的代码中?从我所看到的来看,这是空的
  • 我是怎么错过的,我不知道。谢谢马特的及时回复。添加对象后,我的代码现在运行良好。 $md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider

标签: powershell


【解决方案1】:

这个问题的简单答案是您有一个未声明的(空)变量。在这种情况下,它是$md5。从您提出的评论中,这需要在您的代码中的其他地方声明

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

错误是因为您尝试执行不存在的方法。

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() 是空值表达式。输入乱码也会产生同样的效果。

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 允许按照其 StrictMode 的定义发生这种情况

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多