相关疑难解决方法(0)

您无法在空值表达式上调用方法

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

我的.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."
}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

Get-ExecutionPolicy输出Unrestricted.

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

任何帮助表示赞赏.如果这是微不足道的话,我会道歉并继续我的研究.


参考文献:

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

powershell

13
推荐指数
1
解决办法
16万
查看次数

标签 统计

powershell ×1