【发布时间】: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。
是什么导致了这个错误?
我的空值表达式到底是什么?
感谢任何帮助。如果这是微不足道的,我深表歉意,并将继续我的研究。
参考文献:
【问题讨论】:
-
什么是
$md5?该变量不在您显示的代码中?从我所看到的来看,这是空的 -
我是怎么错过的,我不知道。谢谢马特的及时回复。添加对象后,我的代码现在运行良好。
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
标签: powershell